簡體   English   中英

使用php和mysql從所有用戶的朋友中檢索用戶配置文件數據

[英]Retrieve user profile data from all of a user's friends using php and mysql

我在網上搜索了幾個示例,但在研究后仍然需要一些幫助。 我有2個表:用戶和朋友。

users表包含以下列:

  • id bigint(20)UNSIGNED AUTO_INCREMENT
  • 用戶名varchar(50)
  • profile_picture varchar(200)
  • 狀態varchar(20)
  • status_message varchar(200)

朋友表具有以下列:

  • id bigint(20)UNSIGNED AUTO_INCREMENT
  • Initiator_User_ID Bigint(20)
  • friend_user_id bigint(20)

我希望能夠從作為指定用戶(initiator_user_id)的朋友(friend_user_id)的所有用戶中獲取用戶名,profile_picture,狀態,status_message。 經過研究,我相信這是通過工會完成的。

我已經嘗試過下面的查詢,但是不起作用:

SELECT friend_user_id 
FROM friends
WHERE initiator_user_id = 1001 //gets all the id's of the friends of user 1001
UNION All
SELECT profile_picture, status, status_message, username from users
WHERE friend_user_id = id //gets all the profile data of user's profiles that are friends of user 1001

我需要的是要向所有朋友顯示朋友數據:

  • id bigint(20)UNSIGNED AUTO_INCREMENT
  • 用戶名varchar(50)
  • profile_picture varchar(200)
  • 狀態varchar(20)
  • status_message varchar(200)

我的問題:

  • 是否需要對表進行任何更改(任何外鍵約束...等)?
  • 為了使此示例正常工作,我需要更改什么?

使用簡單的INNER JOIN:

SELECT u.*
FROM users AS u
INNER JOIN friends AS f ON f.friend_user_id = u.id
WHERE f.initiator_user_id = 1001

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM