[英]UNION ALL 2 tables but different columns
I am trying to make a union from 2 tables. 我正在尝试从2张桌子组成一个联合。 Both contains same columns except one of them.
两者都包含相同的列,但其中之一除外。
For example: 例如:
Table A: 表A:
Name, surname, phone 姓名,姓氏,电话
Table B: 表B:
Name, surname, otherColumn 名称,姓氏,其他
I want to make a union from both tables and order them by surname column. 我想从两个表中建立一个联合,并按姓氏列对其进行排序。 It works fine.
工作正常。
But, now I want to know when a row comes from the first table or the second. 但是,现在我想知道何时一行来自第一个表或第二个表。 I thougth result would have Name, surname, phone, otherColumn but just have phone and in that column keeps both results.
我认为结果将包含Name,sname,phone,otherColumn,但仅包含phone,并且在该列中保留两个结果。
Is there a way to keep both columns and fill with nulls when it doesn't exist? 有没有办法保留这两列并在不存在时用空值填充?
Now, this is my query: 现在,这是我的查询:
SELECT id, name, surname ai, phone FROM tableA WHERE status = 0
UNION ALL
SELECT id, name, surname ai, other FROM signedupLocal WHERE status = 0
ORDER BY ai ASC
Try This... 尝试这个...
SELECT id, name, surname ai,phone, NULL as other FROM tableA WHERE status = 0
UNION ALL
SELECT id, name, surname ai, NULL as phone, other FROM signedupLocal WHERE status = 0
ORDER BY ai ASC
You can create a blank temporary column for the field that is missing in the other table like 您可以为其他表格中缺少的字段创建空白的临时列,例如
SELECT id, name, surname, phone, '' as other FROM TableA WHERE status = 0
UNION ALL
SELECT id, name, surname,'' as phone,otherColumn FROM TableB WHERE status = 0
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.