这个问题已经有了答案:
How do I Concatenate entire result sets in MySQL?
8个答案
我有很多这样的问题:

select id,name from phonebook WHERE (name='') AND (Tag ='') AND Country= '';
select id,name from phonebook WHERE (name='') AND Country= '';
select id,name from phonebook WHERE (tag='') AND Country= '';

我想在一个查询中加入它们(按此顺序)。
艾德
我希望在每个查询中按顺序排列(并非全部):
select id,name from phonebook WHERE (name='') AND (Tag ='') AND Country= '' order by points;

所以谁在每个查询中得到更高的分数

最佳答案

如果您希望加入多个查询的结果,或者希望仅实现您的解决方案,那么您可能正在寻找联合(尽管需要注意顺序):

select id,name from phonebook WHERE (name='') AND (Tag ='') AND Country= '' UNION
select id,name from phonebook WHERE (name='') AND Country= '' UNION
select id,name from phonebook WHERE (tag='') AND Country= '';

正如matbailie在comment中建议的那样,如果希望保留每个查询,请使用UNION ALL而不是UNION
Matbailie:UNION将搜索结果并删除重复项。UNION ALL甚至都不会这么做。在这种情况下,我们预先知道这三个查询的结果不一样,因此UNION ALL将消除一些多余的cpu工作。另外,UNION可能会导致顺序发生变化(重复数据消除),而asUNION ALL不会(在我习惯的rdbmse上)。
您点的菜有两种口味
SELECT * FROM
(
  select id,name from phonebook WHERE (name='') AND (Tag ='') AND Country= '' order by Points
) DUMMY_ALIAS1

UNION ALL

SELECT * FROM
(
  select id,name from phonebook WHERE (name='') AND Country= '' order by Points
) DUMMY_ALIAS2

UNION ALL

SELECT * FROM
(
  select id,name from phonebook WHERE (tag='') AND Country= '' order by Points
) DUMMY_ALIAS3

或者
select id,name, points from phonebook WHERE (name='') AND (Tag ='') AND Country= '' UNION
select id,name, points from phonebook WHERE (name='') AND Country= '' UNION
select id,name, points from phonebook WHERE (tag='') AND Country= '' order by Points;

关于php - 在一个查询mysql中加入多个查询,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/23130745/

10-11 19:56