enchance
enchance

Reputation: 30421

Get column from 2 tables and sort by date

I have 2 tables both containing an event and date column. Is there a way to combine the results of both column's event field into one and sort them by their date field. That way only a single (and combined) event is returned instead of 2.

Upvotes: 0

Views: 108

Answers (2)

Utku Yıldırım
Utku Yıldırım

Reputation: 2277

SELECT event,date FROM table1
UNION
SELECT event,date FROM table2 ORDER BY date

When using UNION you use ORDER by at bottom query it will order marged query

You can't use it except bottom query anyway it should throw an error

Upvotes: 3

John Woo
John Woo

Reputation: 263723

SELECT  a.event, MAX(a.date) date
FROM
        (
           SELECT event, date FROM TableA
           UNION
           SELECT event, date FROM TableB
        ) a
GROUP   BY a.event
ORDER   BY a.date DESC

Upvotes: 0

Related Questions