0

首先,出于分类目的,有人能想出一个更好的名字来索引它吗?

其次,我有一个表,我想将用户名从另一个表中抓取到同一张表的两列中。

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
4

2 回答 2

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;
于 2013-10-21T14:47:24.977 回答
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
于 2013-10-21T14:48:14.997 回答