我有两张桌子,第一张名为' posts'第二个叫朋友' 我希望得到它的发布者'栏目存在于“朋友”中。表格(这意味着要获得朋友的帖子)比我想通过在'帖子中找到的日期时间列来订购它表
我试过的是:
$fetch_fr_result = $conn->query("SELECT frid FROM friends WHERE username='$userusername'");
while ($fetch_fr_send = $fetch_fr_result->fetch(PDO::FETCH_OBJ)){
$fetch_fr_id = $fetch_fr_send->frid;
$fetch_fr_data_result = $conn->query("SELECT username FROM users WHERE id='$fetch_fr_id'");
$fetch_fr_data_send = $fetch_fr_data_result->fetch(PDO::FETCH_OBJ);
$fetch_fr_data_username = $fetch_fr_data_send->username;
$fetch_posts_result = $conn->query("SELECT * FROM posts WHERE publisher='$fetch_fr_data_username' ORDER BY orderdate DESC LIMIT 5");
while ($fetch_posts_send = $fetch_posts_result->fetch(PDO::FETCH_OBJ)){
$fetch_posts_msg = $fetch_posts_send->thetext;
$fetch_posts_time = $fetch_posts_send->time;
if (!empty($fetch_posts_msg)){
echo '<div class="posttext">' . $fetch_posts_msg . '</div> - ' . $fetch_posts_time;
}
}
}
此代码中的问题是: 然后让每个朋友获得朋友的帖子,然后按时间排序,但我想按时间点好所有朋友的帖子!
告诉我该怎么做?
答案 0 :(得分:0)
也许我误解了你的问题,但这可能会给你你想要的东西。如果没有告诉我什么是错的,我会尽力解决。
$fetch_result = $conn->query('
SELECT
`p`.thetext,
`p`.time
FROM
`friends` as f,
`users` as u,
`posts` as p
WHERE
`f`.frid = `u`.id /* join friends id with users table */
AND
`p`.publisher = `u`.username /* join friends username with posts table */
AND
`f`.username = "' . $userusername . '"
ORDER BY
`p`.orderdate DESC
');
我建议你在其他表中使用userid作为外键,而不是用户名。您应该更改数据类型以使用更适合的东西。表中的varchar(1000)太多了。