View Full Version : Add column to query result


mouse88
05-06-2009, 12:40 PM
I have the following sql statement:


SELECT * FROM Autoglass UNION ALL SELECT * FROM AutoscreenNetwork UNION ALL SELECT * FROM Autowindscreens UNION ALL SELECT * FROM Glasscare UNION ALL SELECT * FROM MobileWindscreens UNION ALL SELECT * FROM NationalWindscreens


This just unions all records from all tables, however what im looking to do is have a column added to the result table which will contain the name of the table each record came from.

Is this possible?

Appreciate any help

pbaldy
05-06-2009, 12:45 PM
Sure:

SELECT *, "Autoglass" AS SourceTable
FROM Autoglass
UNION ALL
SELECT * , "AutoscreenNetwork" AS SourceTable
FROM AutoscreenNetwork
...

pbaldy
05-06-2009, 12:46 PM
I should add that having all those tables is probably a normalization issue. I'd expect one table with a field identifying whatever the tables now represent.