Search code examples
mysqlfieldunionrecordbelongs-to

MySQL : How to know which table that record belongs to in UNION result


Lets say that I want to get the records from 2 table by using UNION. How could I add a field to each record that would tell me which table it belongs to? It just something like this :

id     |    title     | link            | table
-----------------------------------------------------
1      | Title 1      | somelink.html   | articles1
2      | Title 2      | link2   .html   | articles2
3      | Title 3      | link3   .html   | articles1

Thanks in advance?


Solution

  • select some_column, 'union_1' as from_where
    from table1
    union 
    select some_column, 'union_2' as from_where
    from table2