0

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

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

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

4

5 回答 5

3

你应该尝试像 JOIN

select u.uid,u.name 
   from user u join user_freinds uf on u.uid=uf.fid 
        where u.uid=567445724
于 2011-04-30T16:04:02.013 回答
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;

于 2011-04-30T16:03:26.120 回答
0

您必须使用in关键字。

SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);
于 2011-04-30T16:03:27.263 回答
0

使用 IN 而不是等于:

SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);
于 2011-04-30T16:03:59.540 回答
0
select user.uid, user.name from user, user_friends 
where user.uid=user_friends.fid 
  and user.uid=567445724
于 2011-04-30T16:08:50.023 回答