繁体   English   中英

SQL-检索朋友的用户数据

[英]SQL - Retrieve user data of friends

我想用用户名,性别等显示用户的朋友列表。用户注册后,他的存储在此表中:

USER |id |username |age |gender

如果他添加了朋友,则将友谊插入到我的数据库中,如下所示:

FRIENDS
friendShipId
userId (the id from the user who is logged in and want to show his friends)
friend (the id of the user who the currently loggedInUser is friends with)

现在,我需要获取已登录用户的朋友列表:

SELECT users.username, users.age, users.gender, friends.friendshipId, friends.friend 
FROM friends INNER JOIN users 
ON friends.userId = users.id 
WHERE friends.userId = $id"

如果用户有例如2个朋友,则查询将响应2行,但同时显示当前登录用户的用户数据。 没有他朋友的数据。 你可以帮帮我吗?

我认为您希望join friend列而不是userId列:

SELECT u.username, u.age, u.gender, f.friendshipId, f.friend 
FROM friends f INNER JOIN
     users u       
     ON f.friend = u.id 
WHERE f.userId = $id";

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM