如何查询一个用户所有好友的帖子(存储在3个MySQL表之间)?
How to query posts by all friends of a user (stored between 3 MySQL tables)?
所以我有用户信息、用户发布的内容和朋友都在单独的 tables 中。
示例数据:
用户:
id username email
1 userA myemail@testa.com
2 userB myemail@testb.com
3 userC myemail@testc.com
user_content
id user_id date text
1 1 2015-09-12 00:24:08 content here
2 2 2015-09-11 00:24:08 more content here a
3 1 2015-09-10 00:24:08 more content here b
4 3 2015-09-05 00:24:08 more content here c
朋友
id user_id_1 user_id_2
1 1 2
2 2 3
3 2 4
user
table 存储有关特定用户的信息,user_content
存储该用户发布的内容,friends
存储 'friend' 用户之间的关联。
我的问题是,我如何查询此数据以获得按日期排序的特定用户的所有朋友的 user_content?目前我遍历特定用户的每个朋友:
$stmt = $mysqli->prepare("SELECT user_id_1, user_id_2 FROM friends WHERE user_id_1 = ? OR user_id_2 = ?");
$stmt->bind_param("ii", $userID, $userID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($db_user_id_1, $db_user_id_2);
while ($stmt->fetch()) {
//Work out which is the user, and which is the friend
$friendID = $db_user_id_1;
if ($userID == $db_user_id_1) {
$friendID = $db_user_id_2;
}
//Load latest 10 user_content for each user, store in memory and sort by date
}
$stmt->close();
但这在单个 MySQL 查询中可能吗?遍历每个朋友然后按日期排序的开销似乎有点过分了。我只想要特定用户的所有朋友的最新 30 user_content
。
您需要的是 LEFT JOIN
。
SELECT u.`username`, u.`email`, c.`text`, c.`date`
FROM user AS u
LEFT JOIN friends AS f ON f.`user_id_1` = u.`id`
LEFT JOIN user_content AS c ON c.`user_id` = f.`user_id_2`
WHERE u.`username` = 'userA'
ORDER BY c.`date` ASC
LIMIT 0,30
这将根据您的条件连接 table friends
、user
和 user_content
。
我没有完全理解你的目标。
但这是我获取某些 ?
用户朋友的所有内容的方法:
http://sqlfiddle.com/#!9/fb40e/2
SELECT uc.*
FROM user_content uc
INNER JOIN friends f
ON f.user_id_2 = uc.user_id
AND f.user_id_1 = ?
UPDATE 如果您不确定哪个字段是 uder_id
哪个字段是 friend_id
您可以使用 OR
进行转换:
http://sqlfiddle.com/#!9/fb40e/3
SELECT uc.*
FROM user_content uc
INNER JOIN friends f
ON (f.user_id_2 = uc.user_id
AND f.user_id_1 = ?)
OR (f.user_id_1 = uc.user_id
AND f.user_id_2 = ?)
Select from user_content inner joined on friends,抓取任何一个用户的所有内容,其中 user_content 记录不属于当前用户,但当前用户是其中之一朋友...或者只是看看查询
编辑: 忘记添加最后 30 个帖子的限制
SELECT
uc.user_id AS friend_user_id
,u.username AS friend_username
,u.email AS friend_email
,uc.date
,uc.text
FROM user_content uc
INNER JOIN friends f ON uc.user_id IN (f.user_id_1,f.user_id_2)
INNER JOIN user u ON uc.user_id = u.id
WHERE uc.user_id != ? AND ? IN (f.user_id_1, f.user_id2)
ORDER BY date DESC
LIMIT 30;
所以我有用户信息、用户发布的内容和朋友都在单独的 tables 中。
示例数据:
用户:
id username email
1 userA myemail@testa.com
2 userB myemail@testb.com
3 userC myemail@testc.com
user_content
id user_id date text
1 1 2015-09-12 00:24:08 content here
2 2 2015-09-11 00:24:08 more content here a
3 1 2015-09-10 00:24:08 more content here b
4 3 2015-09-05 00:24:08 more content here c
朋友
id user_id_1 user_id_2
1 1 2
2 2 3
3 2 4
user
table 存储有关特定用户的信息,user_content
存储该用户发布的内容,friends
存储 'friend' 用户之间的关联。
我的问题是,我如何查询此数据以获得按日期排序的特定用户的所有朋友的 user_content?目前我遍历特定用户的每个朋友:
$stmt = $mysqli->prepare("SELECT user_id_1, user_id_2 FROM friends WHERE user_id_1 = ? OR user_id_2 = ?");
$stmt->bind_param("ii", $userID, $userID);
$stmt->execute();
$stmt->store_result();
$stmt->bind_result($db_user_id_1, $db_user_id_2);
while ($stmt->fetch()) {
//Work out which is the user, and which is the friend
$friendID = $db_user_id_1;
if ($userID == $db_user_id_1) {
$friendID = $db_user_id_2;
}
//Load latest 10 user_content for each user, store in memory and sort by date
}
$stmt->close();
但这在单个 MySQL 查询中可能吗?遍历每个朋友然后按日期排序的开销似乎有点过分了。我只想要特定用户的所有朋友的最新 30 user_content
。
您需要的是 LEFT JOIN
。
SELECT u.`username`, u.`email`, c.`text`, c.`date`
FROM user AS u
LEFT JOIN friends AS f ON f.`user_id_1` = u.`id`
LEFT JOIN user_content AS c ON c.`user_id` = f.`user_id_2`
WHERE u.`username` = 'userA'
ORDER BY c.`date` ASC
LIMIT 0,30
这将根据您的条件连接 table friends
、user
和 user_content
。
我没有完全理解你的目标。
但这是我获取某些 ?
用户朋友的所有内容的方法:
http://sqlfiddle.com/#!9/fb40e/2
SELECT uc.*
FROM user_content uc
INNER JOIN friends f
ON f.user_id_2 = uc.user_id
AND f.user_id_1 = ?
UPDATE 如果您不确定哪个字段是 uder_id
哪个字段是 friend_id
您可以使用 OR
进行转换:
http://sqlfiddle.com/#!9/fb40e/3
SELECT uc.*
FROM user_content uc
INNER JOIN friends f
ON (f.user_id_2 = uc.user_id
AND f.user_id_1 = ?)
OR (f.user_id_1 = uc.user_id
AND f.user_id_2 = ?)
Select from user_content inner joined on friends,抓取任何一个用户的所有内容,其中 user_content 记录不属于当前用户,但当前用户是其中之一朋友...或者只是看看查询
编辑: 忘记添加最后 30 个帖子的限制
SELECT
uc.user_id AS friend_user_id
,u.username AS friend_username
,u.email AS friend_email
,uc.date
,uc.text
FROM user_content uc
INNER JOIN friends f ON uc.user_id IN (f.user_id_1,f.user_id_2)
INNER JOIN user u ON uc.user_id = u.id
WHERE uc.user_id != ? AND ? IN (f.user_id_1, f.user_id2)
ORDER BY date DESC
LIMIT 30;