Select 使用 UNION ALL 将表名显示为附加列
Select with UNION ALL to display tablename as an additional column
对于 MSSQL 或 MySQL,这里是一个将两个结果合并为一个的查询:
SELECT boy as person from table1
union all
SELECT girl as person from table2
如何修改上面的查询,使结果包含名称为 table 的第二个(添加的)列(因此它包含 table1
或 table2
值)。
您可以将带有 table 名称的字符串横向作为第二列
SELECT boy as person, 'table1' as column2 from table1
union all
SELECT girl as person, 'table2' as column2 from table2
您需要将它们添加为文字:
SELECT boy as person, 'table1' as tablename from table1
UNION ALL
SELECT girl as person, 'table2' from table2
只是 Hard code
第二列中的 tablename
SELECT boy as person,'Table1' as Tablename from table1
UNION ALL
SELECT girl as person ,'Table2' as Tablename from table2
对于 MSSQL 或 MySQL,这里是一个将两个结果合并为一个的查询:
SELECT boy as person from table1
union all
SELECT girl as person from table2
如何修改上面的查询,使结果包含名称为 table 的第二个(添加的)列(因此它包含 table1
或 table2
值)。
您可以将带有 table 名称的字符串横向作为第二列
SELECT boy as person, 'table1' as column2 from table1
union all
SELECT girl as person, 'table2' as column2 from table2
您需要将它们添加为文字:
SELECT boy as person, 'table1' as tablename from table1
UNION ALL
SELECT girl as person, 'table2' from table2
只是 Hard code
第二列中的 tablename
SELECT boy as person,'Table1' as Tablename from table1
UNION ALL
SELECT girl as person ,'Table2' as Tablename from table2