[英]SQL UNION, check if result is first or second query
I have a SQL query like : 我有一个SQL查询,如:
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
. 通过此查询,我从database1
和database2
table
中检索数据。 Table
is exactly the same for each database. 每个数据库的Table
完全相同。
I put the result into a php variable (big array), but how can I know if a result is from database1
or database2
? 我将结果放入php变量(大数组),但我怎么知道结果是来自database1
还是database2
? How can i add something in SQL for do that ? 我怎样才能在SQL中添加一些内容呢?
Don't use union
, unless you want to remove duplicates. 除非您想删除重复项,否则请勿使用union
。 Use union all
. 使用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";
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.