SELECT t.thread_id, t.subject, u.username,
COUNT(p.post_id) - 1 AS responses,
MAX(DATE_FORMAT(p.posted_on, '%e-%b-%y %l:%i %p')) AS last,
MIN(DATE_FORMAT(p.posted_on, '%e-%b-%y %l:%i %p')) AS first
FROM threads AS t
INNER JOIN posts AS p USING (thread_id)
INNER JOIN users AS u ON t.user_id = u.user_id
WHERE t.cat_id = 1
GROUP BY (p.thread_id) ORDER BY last DESC
我以爲我做了這個權利,但我的查詢只返回一行,我有4行,其中t.cat_id = 1。任何想法?簡單的MYSQL查詢問題
編輯** 樣本數據 http://i56.tinypic.com/f1e449.png
現在我只是想起來了一個結果:
1 THREAD1 USER1 8 17-Apr-11 6:22 AM 17-Apr-11 3:58 AM
我期待得到的東西,如:
1 THREAD1 USER1 8 17-Apr-11 6:22 AM 17-Apr-11 3:58 AM
2 THREAD2 USER1 8 17-Apr-11 6:22 AM 17-Apr-11 3:58 AM
3 THREAD3 USER1 8 17-Apr-11 6:22 AM 17-Apr-11 3:58 AM
你能後的樣本數據,預期與實際結果? – Oded 2011-04-17 07:30:02
也許所有4行都有相同的p.thread_id,你可以通過它做一組 – 2011-04-17 07:30:26
當然,給我一分鐘。 – aph107 2011-04-17 07:30:43