给定这两个表,我需要什么 mysql 才能达到以下结果:
表格1:
+----+-------+
| id | name |
+----+-------+
| 1 | alan |
| 2 | bob |
| 3 | dave |
+----+-------+
表2:
+----+---------+
| id | state |
+----+---------+
| 2 | MI |
| 3 | WV |
| 4 | FL |
+----+---------+
我想创建一个看起来像这样的临时视图
期望的结果:
+----+---------+---------+
| id | name | state |
+----+---------+---------+
| 1 | alan | |
| 2 | bob | MI |
| 3 | dave | WV |
| 4 | | FL |
+----+---------+---------+
我尝试了一个 mysql union 但以下结果不是我想要的。
create view table3 as
(select id,name,"" as state from table1)
union
(select id,"" as name,state from table2)
表3联合结果:
+----+---------+---------+
| id | name | state |
+----+---------+---------+
| 1 | alan | |
| 2 | bob | |
| 3 | dave | |
| 2 | | MI |
| 3 | | WV |
| 4 | | FL |
+----+---------+---------+