موسى دريهم
موسى دريهم

Reputation: 25

Get mysql table name from query result

I want to display statistics about a mysql table using this Query:

select * from t1 where product = ABC
Union
select * from t2 where product = ABC

The above query produces 2 rows with product="ABC" in t1 and 3 rows with product="ABC" in t2. The query doesn't tell me which table it came from.

How can the query be changed so I can know the source table?

Upvotes: 1

Views: 70

Answers (1)

juergen d
juergen d

Reputation: 204904

You can include a unique token in each query to let you know which table the data came from like this:

select *, 't1' as tablename from t1 where product = 'ABC'
Union  
select *, 't2' as tablename from t2 where product = 'ABC'

Upvotes: 2

Related Questions