简体   繁体   English

使用php和mysql从所有用户的朋友中检索用户配置文件数据

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

I searched several examples on the web and still need some help after researching. 我在网上搜索了几个示例,但在研究后仍然需要一些帮助。 I have 2 tables: users and friends. 我有2个表:用户和朋友。

the users table has columns: users表包含以下列:

  • id bigint(20)UNSIGNED AUTO_INCREMENT id bigint(20)UNSIGNED AUTO_INCREMENT
  • username varchar(50) 用户名varchar(50)
  • profile_picture varchar(200) profile_picture varchar(200)
  • status varchar(20) 状态varchar(20)
  • status_message varchar(200) status_message varchar(200)

the friends table has columns: 朋友表具有以下列:

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

I want to be able to get the username, profile_picture, status, status_message from all users who are friends (friend_user_id) of a specified user (initiator_user_id). 我希望能够从作为指定用户(initiator_user_id)的朋友(friend_user_id)的所有用户中获取用户名,profile_picture,状态,status_message。 After researching I believed this is done with a union. 经过研究,我相信这是通过工会完成的。

I have tried the query below but it doesn't work: 我已经尝试过下面的查询,但是不起作用:

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

What I need is for the friends data to be displayed for all friends: 我需要的是要向所有朋友显示朋友数据:

  • id bigint(20)UNSIGNED AUTO_INCREMENT id bigint(20)UNSIGNED AUTO_INCREMENT
  • username varchar(50) 用户名varchar(50)
  • profile_picture varchar(200) profile_picture varchar(200)
  • status varchar(20) 状态varchar(20)
  • status_message varchar(200) status_message varchar(200)

My questions: 我的问题:

  • Does anything need to be changed regarding the tables (any foreign key constraints...etc.)? 是否需要对表进行任何更改(任何外键约束...等)?
  • What do I need to change to make this example work? 为了使此示例正常工作,我需要更改什么?

Use a simple INNER JOIN: 使用简单的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