mysql将一列中多个表的多个列组合在一起

时间:2016-02-24 09:19:27

标签: mysql view

假设我有两个表(表1和表2) 每个表都有两列(id,number),如下所示

table 1

+-----+------------+
|  id |   number   |
+-----+------------+
|  1  |   value1   |
+-----+------------+

table 2
+-----+------------+
|  id |   number   |
+-----+------------+
|  2  |   value2   |
+-----+------------+

我期待的是,只需在两列中选择所有ID和数字即可创建新视图

+-----+------------+
|  id |   number   |
+-----+------------+
|  1  |   value1   |
+-----+------------+
|  2  |   value2   |
+-----+------------+

2 个答案:

答案 0 :(得分:1)

SELECT id, number FROM table1 
UNION ALL
SELECT id, number FROM table2

答案 1 :(得分:1)

您可以使用union all或union获取唯一值,如下所示 -

select id,number from tabl1 
union all 
select id,number from tabl2;