gpt4 book ai didi

mysql - SQL - 检索好友的用户数据

转载 作者:行者123 更新时间:2023-11-29 23:27:30 25 4
gpt4 key购买 nike

我想显示用户的 friend 列表,包括用户名、性别等。用户注册后,他将存储在该表中:

USER |id |username |age |gender

如果他添加了 friend ,友谊就会插入到我的数据库中,如下所示:

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 个 friend ,查询响应 2 行,但均包含当前登录用户的用户数据。不是他 friend 的数据。你能帮我吗?

最佳答案

我认为您希望在 friend 列上加入 join,而不是 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";

关于mysql - SQL - 检索好友的用户数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26844010/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com