我的網站上有一個關注系統,您可以在其中跟蹤其他用戶。查詢使用其他表中的ID獲取最新的10行
在網站的主頁上,我試圖讓它顯示您正在關注的用戶的最新10條信息(而不是您關注的每個人的10條信息,但總共有10個)。
我有一臺名爲followers
結構如下:
id | user_id | following_id
1 20 52
2 20 55
1 20 75
... ... ...
哪裏user_id
是你的ID和follow_id
是您關注的用戶的ID。
然後我有一個名爲posts
的表,其中收集了來自用戶的所有帖子。
我現在要做的是創建一個查詢,從您正在關注的用戶(按日期排序)獲取最新的10條信息。
這是我到目前爲止已經做出:
/* Select all users this person is following */
$stmt = $cxn->prepare('SELECT following_id FROM followers WHERE user_id = ?');
$stmt->bind_param('i', $user_id);
$stmt->execute();
$result = $stmt->get_result();
/* If a result exists, continue. */
if ($result->num_rows) {
while ($row = $result->fetch_assoc()) {
// not sure what to do here, how would the query look?
}
} else {
echo "You aren't following anyone!";
}
我不知道什麼查詢會/應該是從你關注的人一共拿到了最新的帖子10。
請幫忙!
相關:[SQL只選擇最高值在列行(http://stackoverflow.com/q/7745609/1037210) – Lion