-1

I selected data from multiple table with query:

SELECT * FROM TABLE1 
UNION ALL 
SELECT * FROM TABLE2

When I deleting data I want to see to which table the row is belonging to. I want to know is there a way to set table name to each row while retrieving data. I dont want to add new column with table_name to a table. Any Idea?

DorFik
  • 47
  • 5

3 Answers3

1

Add a column with the name of the table:

SELECT 'TABLE1', TABLE1.* FROM TABLE1 
UNION ALL 
SELECT 'TABLE2', TABLE2.* FROM TABLE2
Luuk
  • 12,245
  • 5
  • 22
  • 33
1
SELECT *, "table1" as tablename FROM TABLE1 
UNION ALL 
SELECT *, "table2" FROM TABLE2
buniek
  • 36
  • 1
1
     SELECT t1.*, t1.data FROM TABLE1 t1
         UNION 
         SELECT t2.*, t2.data FROM TABLE2 t2   
    
> If you want to get without duplicate data from two tables then you use UNION


    SELECT t1.*, t1.data FROM TABLE1 t1
        UNION ALL 
        SELECT t2.*, t2.data FROM TABLE2 t2

> If you want to get all data from two tables then you use UNION ALL