Mysql
 sql >> Datenbank >  >> RDS >> Mysql

Wählen Sie bei zwei Tabellen alle Daten aus einer Tabelle und nur die neuesten aus der anderen aus

Finden Sie zuerst den neuesten Beitrag in jeder Kategorie:

select topic_cat, max(topic_id) as latest_topic
from topics group by topic_cat

Dann fügen Sie das zu Ihren Teilnahmebedingungen hinzu:

SELECT  c.cat_name AS Category, t.topic_name AS Recent_Topic 
FROM categories c
left JOIN topics t on c.cat_id = t.topic_cat 
left join (select topic_cat, max(topic_id) as latest_topic
        from topics group by topic_cat) as latest_topics 
        on latest_topics.topic_cat = c.cat_id
        and latest_topics.latest_topic = t.topic_id 
where latest_topics.topic_cat is not null or t.topic_cat is null;