来自不同表的联合列值

Posted

技术标签:

【中文标题】来自不同表的联合列值【英文标题】:Union column value from different tables 【发布时间】:2015-07-15 10:58:33 【问题描述】:

我有三个表:Table1Table2Table3。 这三个表都包含一个键列作为外键。

我想要一个 SQLite 查询从所有三个表中返回所有不同的键列。

例如

表 1:

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

表 2:

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

所以我想要 SQLite 查询以排序顺序返回两个表中的所有不同键

【问题讨论】:

【参考方案1】:

这应该适合你:

select key from table1  union select key from table2;

如果你想重复使用union all

select key from table1  union all select key from table2;

【讨论】:

以上是关于来自不同表的联合列值的主要内容,如果未能解决你的问题,请参考以下文章