Question

Here's the end result of my latest attempt that throws an error (based on answers I've seen here regarding mysql questions)

SELECT result.* FROM ( SELECT p.post_id FROM phpbb_posts p WHERE p.topic_id = 297 limit 1 ORDER BY p.post_time ASC UNION SELECT p.post_id FROM phpbb_posts p WHERE p.topic_id = 297 ORDER BY p.post_time DESC ) result ORDER BY result.id LIMIT 10

Basically I'm trying to grab the first post (order by p.post_time asc limit 1), then append to that a descending order of posts in the topic, to show the latest post first.

There's no support for doing this that I can find at phpbb's forums (yes they will say you can set it in control panel, but that just makes the original post appear at the end of the topic).

Actual code of viewtopic.php for this query (at least I believe it's the correct place to make changes):

$sql = 'SELECT p.post_id
    FROM ' . POSTS_TABLE . ' p' . (($join_user_sql[$sort_key]) ? ', ' . USERS_TABLE .     ' u': '') . "
    WHERE p.topic_id = $topic_id
        " . ((!$auth->acl_get('m_approve', $forum_id)) ? 'AND p.post_approved = 1'     : '') . "
        " . (($join_user_sql[$sort_key]) ? 'AND u.user_id = p.poster_id': '') . "
        $limit_posts_time
    ORDER BY $sql_sort_order";
$result = $db->sql_query_limit($sql, $sql_limit, $sql_start);

I've been at this a while. I'm not lazy, at least not tonight, I'm just tired and out of my depth.

Thanks.

Was it helpful?

Solution

from your first query it looks like you're trying to get the earliest/original post then append 9 latest post order by post_time desc. so then maybe a query like this (sqlFiddle)

SELECT post_id,timeorder FROM
 (SELECT p.post_id,now()as timeorder FROM phpbb_posts p WHERE p.topic_id = 297 ORDER BY    p.post_time ASC LIMIT 1)T1
UNION
SELECT post_id,timeorder FROM
 (SELECT p.post_id,p.post_time as timeorder FROM phpbb_posts p WHERE p.topic_id = 297 
      AND EXISTS(SELECT 1 FROM phpbb_posts p2 WHERE p2.post_time < p.post_time AND p2.topic_id = 297)
    ORDER BY p.post_time DESC LIMIT 9)T2
ORDER BY timeorder DESC

the timeorder in the query is just used for sorting at the end so that the original post is first row and then the rest of the rows are latest posts order by post_time desc

the EXISTS in T2 check is to make sure you don't include the original post(since it's already selected by T1)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top