[英]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表包含以下列:
the friends table has columns: 朋友表具有以下列:
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: 我需要的是要向所有朋友显示朋友数据:
My questions: 我的问题:
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.