我有两张相同的桌子。 我需要以这种方式结合它们:
SELECT f1,f2, xxx
FROM
(SELECT *
FROM tbl1
UNION ALL
SELECT *
FROM tbl2)
其中xxx将查询表名,其中f1和f2字段取自。 示例输出:
123 345 'tbl1' -- this rows are from the first table
121 345 'tbl1'
121 345 'tbl1'
123 345 'tbl1'
124 345 'tbl1'
125 345 'tbl2' -- this rows are from the second table
127 345 'tbl2'
提前谢谢。
答案 0 :(得分:2)
SELECT f1,f2, xxx
FROM
(SELECT *, 'tbl1' as xxx
FROM tbl1
UNION ALL
SELECT *, 'tbl2' as xxx
FROM tbl2)