我正在使用UNION从2行获取连接输出。 A和B,以下是代码。
"SELECT `ent_id` as `id`, `owner_id`, `category_id`, `ent_name` as `name`, `ent_details` as `details` FROM `A` WHERE `category_id` = '$cat'
UNION
SELECT `service_id` as `id`, `owner_id`, `category_id`, `service_name` as `name`, `service_details` as `details` FROM `B` WHERE `category_id` = '$cat'
查询工作绝对正常,但我现在想知道哪个输出来自表A,哪个来自B.
有办法做到这一点吗?如果是这样的话?
感谢您的时间。 :)
答案 0 :(得分:2)
为每个选择添加一个常量值:
select 'table a' as source_table, ... from A where ...
union all
select 'table b' as source_table, ... from B where ...
此外,union
消除了构成union all
没有的联合的两个集之间的重复。如果没有重复项,则应使用union all
以获得更好的性能。