帮助简单的SQL语句

时间:2011-04-30 16:00:44

标签: mysql sql mysql-error-1242

我有一个用户表和user_friend表。 user_friend显示我与谁的朋友(所以它是uid和fid)。我想得到所有朋友的信息,所以我做了:

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

但它给我一个Subquery返回超过1行,我应该如何修改这个查询?

5 个答案:

答案 0 :(得分:3)

您应该尝试像

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

答案 1 :(得分:2)

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

或者,在MySQL中,我相信我记得读到联接通常比子选择更加困难。所以也许:

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

答案 2 :(得分:0)

您必须使用in关键字。

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

答案 3 :(得分:0)

使用IN而不是equals:

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

答案 4 :(得分:0)

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