我有一个用户表和 user_friend 表。user_friend 显示我和谁是朋友(所以它是 uid 和 fid)。我想获取我所有朋友的信息,所以我做了:
SELECT UID, Name FROM user WHERE UID = (SELECT FID FROM user_friend WHERE UID = 567445724);
但它给了我一个子查询返回超过 1 行,我应该如何修改这个查询?
我有一个用户表和 user_friend 表。user_friend 显示我和谁是朋友(所以它是 uid 和 fid)。我想获取我所有朋友的信息,所以我做了:
SELECT UID, Name FROM user WHERE UID = (SELECT FID FROM user_friend WHERE UID = 567445724);
但它给了我一个子查询返回超过 1 行,我应该如何修改这个查询?
你应该尝试像 JOIN
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);`
或者,在 MySQL 中,我相信我记得读过连接通常比子选择快得多。所以也许:
SELECT user.UID, user.Name FROM user, user_friend
WHERE user.UID = user_friend.FID AND user_friend.UID = 567445724;
您必须使用in
关键字。
SELECT UID, Name FROM user WHERE UID IN (SELECT FID FROM user_friend WHERE UID = 567445724);
使用 IN 而不是等于:
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