例如,我试图在sql查询中获取表名,而我编写该查询是为了从同一个表中获取数据表1表2所以在做这个查询的时候
Select * from table_one UNION Select * from table_two
我得到的结果是一个表中两个表的数据如下:但我需要的是,在表中再增加一列,以查看数据来自哪个表,如下所示
xqkwcwgp1#
使用 union all :
union all
select id, name, age, 'table_one' as tabel_namefrom table_oneunion allselect id, name, age, 'table_two'from table_two
select id, name, age, 'table_one' as tabel_name
from table_one
select id, name, age, 'table_two'
from table_two
1条答案
按热度按时间xqkwcwgp1#
使用
union all
: