2013-12-12 19 views
0

我目前使用此代碼根據創建日期在我的論壇中訂購論壇主題。SQL查詢選擇所有論壇主題,但按最新的活動線程排序

$e = mysqli_real_escape_string($_GET["forum"]); 
$q = mysqli_query($con, 
    "SELECT * FROM threads WHERE forum = '$e' ORDER BY date DESC LIMIT $offset,$rowsPerPage"); 

但是,這個順序是線程的創建日期而不是他們的活動。
我怎樣才能按他們的活動排序?

我沿着
SELECT * FROM threads WHERE forum = '$e' ORDER BY (replies in this thread).date DESC LIMIT 5

行思考的東西這是可能的或沒有?

編輯:
table thread holds the inital thread data
table replies holds the replies

+2

很難說沒有你的表結構和示例數據。 –

+1

關於迄今爲止提供的信息?沒有機會。 – Strawberry

+1

您需要有一個updated_at列 –

回答

1

添加含有最後一次線程已回覆一欄改成您threads表,然後通過這個山口使用順序。不要忘記更新新回覆的專欄。

有許多方法可以手動完成,但它比專用列慢得多。

0

子查詢聯接會做的伎倆:

select t.* , lp.latestpost 
from threads as t 
inner join ( 
    select p.thread_id , max(p.date) as latestpost 
    from replies as p 
    group by p.thread_id 
) as lp on pl.thread_id = t.id 
order by lp.latestpost desc 
+0

左連接可能會套件更好。不知道你的問題。 – floww