Union column value from different tables

I have three tables: Table1

, Table2

and Table3

.
All three tables contain a key column as a foreign key.

I want the SQLite query to return all individual key columns from all three tables.

eg.

Table 1:

+---------+---------+
|  Col1   |  Col2   | key |
+---------+---------+
| Val 1   | Val 2   | 100 |
| Val 3   | Val 6   | 101 |
| Val 4   | Val 7   | 103 |
| Val 5   | Val 8   | 104 |
+---------+---------+

      

Table 2:

+---------+---------+
|  Col1   |  Col2   |       key |
+---------+---------+
| Val 1   | Val 2   |       100 |
| Val 3   | Val 6   |       101 |
| Val 4   | Val 7   |       105 |
| Val 50  | Val 18  |       106 |
+---------+---------+

      

So, I want the SQLite query to return all different keys from both tables in sorted order

+3


source to share


1 answer


This should work for you:

select key from table1  union select key from table2;

      




If you want duplicates to use union all

select key from table1  union all select key from table2;

      

+1


source







All Articles