Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to select distinct values from 2 tables with sort in one query?

Tags:

sql

distinct

I have

  • table1 : country, theOrderColumn1
  • table2 : country, theOrderColumn2

I want to join DISTINCT country from these two SELECT statements:

SELECT DISTINCT `country` FROM `table1` ORDER BY `theOrderColumn1`

and

SELECT DISTINCT `country` FROM `table2` ORDER BY `theOrderColumn2`

Example:

table1 (country, theOrderColumn1): (uk, 1), (usa, 2)
table2 (country, theOrderColumn2): (france, 1), (uk, 2)

I want this result:

france
uk
usa
like image 577
ali Avatar asked Sep 28 '11 13:09

ali


1 Answers

select distinct country
from (
    select country, theOrderColumn from table1
    union all
    select country, theOrderColumn from table2
) a 
order by theOrderColumn
like image 112
D'Arcy Rittich Avatar answered Oct 13 '22 00:10

D'Arcy Rittich