对于 MSSQL 或 MySQL,这里是一个将两个结果合并为一个的查询:
SELECT boy as person from table1
union all
SELECT girl as person from table2
如何修改上面的查询,使结果包含带有表名称的第二个(添加的)列(因此它包含 table1
或 table2
值)。
最佳答案
您可以将表名作为第二列的字符串横向
SELECT boy as person, 'table1' as column2 from table1
union all
SELECT girl as person, 'table2' as column2 from table2
关于mysql - 使用 UNION ALL 选择以将表名显示为附加列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27967562/