2013-05-20 4 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 

Je voudrais lister les 3 premiers résultats, et puisque les 3 derniers commentaires étaient dans le même sujet, je reçois le même sujet 3 fois. Ni DISTINCT ni GROUP BY ne semblent fonctionner.Utiliser DISTINCT joignant correctement plusieurs tables

+1

peut vous donner des enregistrements d'échantillons où nous pouvons le tester? et aussi votre résultat souhaité. –

Répondre

1

Essayez:

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 
Questions connexes