查询问题

时间:2010-11-17 18:47:44

标签: mysql

我在查询工作时遇到了一些麻烦。

它想要做的是找到论坛及其子论坛上最后一篇文章的信息。如果最后一篇文章是在该论坛中发布的,那么它是有效的,但如果它是在子论坛中制作的,则它无法正常工作。

我觉得问题出在WHERE周围,但我不太确定......

有什么想法吗?

// Last Post Info
$lastpost_sql = "
    SELECT
        forum_posts.post_id AS post_id,
        forum_posts.post_date AS post_date,
        forum_posts.thread_id AS thread_id,
        temp.thread_title AS thread_title,
        temp.forum_id AS forum_id,
        users.user_id AS user_id,
        users.user_firstname AS user_firstname,
        users.user_lastname AS user_lastname
    FROM (
        SELECT
            forum_threads.thread_id,
            forum_threads.thread_title,
            forum_threads.forum_id
        FROM
            forums AS t1
            LEFT JOIN
                forums AS t2 ON t2.forum_parent = t1.forum_id
            LEFT JOIN
                forums AS t3 ON t3.forum_parent = t2.forum_id
            INNER JOIN
                forum_threads ON forum_threads.forum_id = t1.forum_id OR forum_threads.forum_id = t2.forum_id OR forum_threads.forum_id = t3.forum_id
        WHERE
            t1.forum_id = '$forum2[id]'
        GROUP BY
            forum_threads.thread_id
    )
    AS
        temp
    INNER JOIN
        forum_posts ON forum_posts.thread_id = temp.thread_id
    INNER JOIN
        users ON forum_posts.user_id = users.user_id
    ORDER BY
        forum_posts.post_id DESC LIMIT 1
";

1 个答案:

答案 0 :(得分:0)

我认为这是因为你的子查询。

试试这个:

SELECT
    thread_id,
    thread_title,
    forum_id
FROM
    forum_threads 
WHERE
    forum_id = '$forum2[id]'
    OR forum_id = (SELECT forum_parent 
                    FROM forums 
                    WHERE forum_id = '$forum2[id]')
    OR forum_id = (SELECT forum_parent 
                    FROM forums 
                    WHERE forum_id = (SELECT forum_parent 
                                        FROM forums 
                                        WHERE forum_id = '$forum2[id]'))

这应该在子论坛深入两级并抓住每个帖子。