'UNION ALL define table

Can you please tell me how can I determine from which table this id is derived? SELECT id FROM table_1 UNION SELECT id FROM table_2

table_1
|id|
|1 |
table_2
|id|
|2 |

Through while I got two id (1, 2). How to understand in which table id 1 and in which table id 2?



Solution 1:[1]

You could inject your own identifier, such as:

SELECT id, 'T1' as Source
FROM table_1 
UNION 
SELECT id, 'T2'
FROM table_2

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Stu