开发者

Help with simple SQL statement

I have a user table and user_friend table. 开发者_开发技巧user_friend shows who I am friend with (so it's uid and fid). I want to get the info of all my friends, so I did:

SELECT UID, Name FROM user WHERE UID = (SELECT FID FROM user_friend WHERE UID = 567445724);

but it gives me a Subquery returns more than 1 row, how should I modify this query?


You should try JOIN like

select u.uid,u.name 
   from user u join user_freinds uf on u.uid=uf.fid 
        where u.uid=567445724


SELECT UID, Name FROM user
WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);`

Alternatively, in MySQL, I believe I recall reading that joins are often much fasdter than subselects. So perhaps:

SELECT user.UID, user.Name FROM user, user_friend WHERE user.UID = user_friend.FID AND user_friend.UID = 567445724;


You have to use the in keyword.

SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);


Use IN instead of equals:

SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);


select user.uid, user.name from user, user_friends 
where user.uid=user_friends.fid 
  and user.uid=567445724
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜