在MySQL中使用UNION时区分输出
我正在使用UNION从两行获取连接输出。A和B,下面是代码在MySQL中使用UNION时区分输出,mysql,Mysql,我正在使用UNION从两行获取连接输出。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`
"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
有办法做到这一点吗?如果是,怎么做
谢谢您的时间。:) 为每个选择添加一个常量值:
select 'table a' as source_table, ... from A where ...
union all
select 'table b' as source_table, ... from B where ...
另外,
union
消除了构成union的两个集合之间的重复项,而union all
则没有。如果没有重复项,则应使用union all
以获得更好的性能。为每个选择项添加一个常量值:
select 'table a' as source_table, ... from A where ...
union all
select 'table b' as source_table, ... from B where ...
另外,union
消除了构成union的两个集合之间的重复项,而union all
则没有。如果没有重复项,则应使用union all
以获得更好的性能