SQL query JOINED Data from which Table?

A

Abdul

Hello All,

I am using a query to get data from two tables using UNION All.

How can I add a field to the joined data to show the data from which
table?

Something like For eg.

If I say select field1, field2, field 3 FROM Table1 Union all field1,
field2, field3 FROM Table2

Then can I add a field say field4 (which is not in either table) and
add "A" if the data is from Table1 and "B" if the data from Table2 ?

Thanks
 
A

Ardus Petus

SELECT field1, field2, field3, 'A' as tableId FROM Table1
UNION ALL
SELECT field1, field2, field3, 'B' as tableId FROM Table2


HTH
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top