1

我有三张桌子:

  1. post(id) - 包含所有条目
  2. tag(id) - 拥有所有标签
  3. assign(post_id,tag_id) - 为帖子分配标签,一个帖子可能有多行

这是我到目前为止所拥有的:

$sql_post = mysql_query("SELECT title,content,time,web FROM post ORDER BY time DESC LIMIT 5");

$sql_tag = mysql_query("SELECT tag.name,tag.id,post.id FROM assign LEFT JOIN tag ON assign.tag_id=tag.id LEFT JOIN post ON assign.post_id=post.id WHERE post.time>(SELECT MIN (time) FROM (SELECT time FROM post ORDER BY time DESC LIMIT 6) 二)");

我正在考虑将它们全部放入一个大数组中,所以像这样(草率的总结):

array(
    post_id => array(
        "title" => "post's title...",
        "content" => "post's content...",
        "tag" => array(
            tag_id => "tag 1's name",
            tag_id => "tag 2's name

...然后使用 foreach 将它们显示在我的博客上。

我需要帮助的地方是 .sql 文件$sql_tag。我如何使它选择最近 5 个帖子的所有作业?它只为帖子选择一个标签。

4

2 回答 2

0

为什么不循环浏览您感兴趣的帖子?

$sql_post = mysql_query("SELECT id, title,content,time,web FROM post ORDER BY time DESC LIMIT 5");

while ($row = mysql_fetch_assoc($sql_post)) {
     $sql_tag = mysql_query("SELECT tag.name,post.id FROM assign LEFT JOIN tag ON assign.tag_id=tag.id WHERE assign.post_id='".$row['id']."'");
}
于 2012-06-17T19:13:06.027 回答
0

尝试:

SELECT
    c.id,
    c.name,
    a.id
FROM
    (
        SELECT id
        FROM post
        ORDER BY time DESC
        LIMIT 5
    ) a
LEFT JOIN
    assign b ON a.id = b.post_id
LEFT JOIN
    tag c ON b.tag_id = c.id
于 2012-06-17T19:31:06.253 回答