首先,出于分类的目的,有人可能想出一个更好的名称来将其编入索引吗?
其次,我有一个表,我想从另一个表中获取用户名到同一个表上的两列。
SELECT ignite_board.*,username AS 'name'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
ORDER BY POSITION
给我这个:
ID bnumber position UID qual time following name
1176 3 1 442 2 1382045726 440 bb1
1177 3 2 445 2 1382045936 442 bb4
1181 3 3 450 2 1382050220 449 bb6
1178 3 4 446 2 1382050371 439 aa2
1209 3 5 466 1 1382050130 450 bb8
1212 3 6 469 2 1382050502 467 bb10
1210 3 7 467 1 1382050172 466 bb9
1232 3 8 475 1 1382050415 446 aa7
1180 3 9 444 0 1382045690 445 bb3
1214 3 10 471 0 1382050220 466 bb11
1233 3 11 476 0 1382050415 475 aa8
1179 3 12 441 0 1382045562 475 aa1
1182 3 13 452 0 1382046032 448 aa6
1216 3 14 473 0 1382050272 469 bb12
1234 3 15 477 0 1382050502 469 bb16
1271 3 16 454 0 1382306814 442 bb7
但是我需要获取用户名和以下用户名(这样的排序)
SELECT ignite_board.*,username AS 'name',username AS 'following'
FROM ignite_board,user_info
WHERE bnumber = 3
AND ignite_board.uid = user_info.id
AND ignite_board.following = user_info.id
ORDER BY POSITION
答案 0 :(得分:1)
SELECT b.*
, u.username name
, f.username following
FROM ignite_board b
JOIN user_info u
ON u.id = b.uid
JOIN user_info f
ON f.id = b.following
WHERE bnumber = 3
ORDER
BY position;
答案 1 :(得分:1)
您必须两次加入user_info
表,并为每个实例提供不同的别名:
SELECT ignite_board.*,u.username AS 'name',f.username AS 'following'
FROM ignite_board,user_info u, user_info f
WHERE bnumber = 3
AND ignite_board.uid = u.id
AND ignite_board.following = f.id
ORDER BY POSITION