Alex Rivera | Logout

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

Asked 2011-09-28T13:38:26.970
17

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
Edit
Report

1 Answer

2
select country, theOrderColumn from (
select distinct t1.country as country, t1.theOrderColumn as theOrderColumn from table t1
union
select distinct t2.country as country, t2.theOrderColumn as theOrderColumn from table t2) t3
order by theOrderColumn
answered 2011-09-28T13:42:58.010

Your Answer