2013-05-20 49 views
0
SELECT * FROM f_posts 
INNER JOIN f_topics 
ON f_posts.post_topic=f_topics.topic_id 
INNER JOIN f_subcategories 
ON f_subcategories.scat_id=f_topics.topic_scat 
ORDER BY f_posts.post_date 
DESC LIMIT 0,3 

我想列出前3個結果,並且由於最後3個評論屬於同一主題,所以我獲得相同主題3次。 DISTINCT和GROUP BY似乎都不起作用。使用DISTINCT正確加入多個表格

+1

你可以給樣本記錄,我們可以測試它?也是你想要的結果。 –

回答

1

嘗試:

SELECT p.* , t.*, s.* 
FROM f_posts p 
INNER JOIN (select post_topic, max(post_date) max_date 
      from f_posts 
      group by post_topic 
      order by max_date desc LIMIT 0,3) m 
     ON p.post_topic = m.post_topic and p.post_date = m.max_date 
INNER JOIN f_topics t ON p.post_topic=t.topic_id 
INNER JOIN f_subcategories s ON s.scat_id=t.topic_scat 
ORDER BY f_posts.post_date DESC