I have two same tables. I need to union them in such way:
SELECT f1,f2, xxx
FROM
(SELECT *
FROM tbl1
UNION ALL
SELECT *
FROM tbl2)
where xxx would query for a table name开发者_运维问答, where f1 and f2 fields are taken from. Example output:
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'
Thank you in advance.
SELECT f1,f2, xxx
FROM
(SELECT *, 'tbl1' as xxx
FROM tbl1
UNION ALL
SELECT *, 'tbl2' as xxx
FROM tbl2)
精彩评论