对于MSSQL或MySQL,这是一个将两个结果合二为一的查询:
SELECT boy as person from table1
union all
SELECT girl as person from table2
如何修改上面的查询,以便结果包含带有表名称的第二个(已添加)列(因此它包含table1
或table2
值。)
答案 0 :(得分:4)
你可以给表格名称作为第二栏的字符串横向
SELECT boy as person, 'table1' as column2 from table1
union all
SELECT girl as person, 'table2' as column2 from table2
答案 1 :(得分:3)
只需Hard code
第二栏中的tablename
SELECT boy as person,'Table1' as Tablename from table1
UNION ALL
SELECT girl as person ,'Table2' as Tablename from table2
答案 2 :(得分:1)
您需要将它们添加为文字:
SELECT boy as person, 'table1' as tablename from table1
UNION ALL
SELECT girl as person, 'table2' from table2