«

»

Feb 08

Beitrag drucken

SQL, UNION and correct ORDER

Just had to combine two tables month column and sort the whole thing. I had two tables like

Name Month
Something 201201
Else 201112

and

Name Month
Anything 201201
Other 201111

and I wanted (put the two tables‘ month in one column and remove duplicates):

Month
201201
201112
201111

The solution was not too complicated, but I thought I share it.

SELECT MONTH FROM table1
UNION
SELECT MONTH FROM table2 ORDER BY MONTH DESC

No need to add a GROUP BY because of the UNION, and the seconds query ORDER BY sorts the complete result.

Permanentlink zu diesem Beitrag: http://techblog.steffmeister.at/sql-union-and-correct-order/

Schreibe einen Kommentar

Deine E-Mail-Adresse wird nicht veröffentlicht. Erforderliche Felder sind mit * markiert.

Du kannst folgende HTML-Tags benutzen: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>