[英]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.