I need UNION two tables with creating new field, where 1 for first table, and 2 for second.
I tried
(
SELECT field, 1 AS tmp
FROM table1
)
UNION
(
SELECT field, 2 AS tmp
FROM table2
)
But in result, tmp field was full of “1”.
How it can be implemented?
Your query should work fine. The only thing you should change is
UNIONshould beUNION ALLto give better performance. Without theALLit defaults toUNION DISTINCTwhich causes the rows to be compared for duplicates*, but the way you have constructed them guarantees that there cannot be duplicates so this extra check is a waste of time. Here is some test code I used to verify that what you are doing ought to work:Result:
If you only get rows where tmp was equal to 1, maybe your table2 was empty?
*See the documentation for UNION.