简体   繁体   中英

SQL UNION, check if result is first or second query

I have a SQL query like :

SELECT * FROM database1.table WHERE "_CONDITIONS" 
UNION 
SELECT * FROM database2.table WHERE "_CONDITIONS"

With this query, I retrieve datas from table in database1 and database2 . Table is exactly the same for each database.

I put the result into a php variable (big array), but how can I know if a result is from database1 or database2 ? How can i add something in SQL for do that ?

Don't use union , unless you want to remove duplicates. Use union all . And then just add a column:

SELECT t.*, 1 as which FROM database1.table t WHERE "_CONDITIONS"
UNION ALL
SELECT t.*, 2 as which FROM database2.table t WHERE "_CONDITIONS";

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM