我有多个这样的查询:
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= '';
我想将它们加入一个查询(按此顺序)。
EID
我想在每个查询(不是所有查询)中放置顺序:
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 在评论中所建议,如果您希望重复导致每个查询保留而不是使用 UNION ALL
而不是 UNION
.
MatBailie:UNION
将搜索结果并删除重复项。 UNION ALL
甚至不会尝试。在这种情况下,我们事先知道三个查询没有相同的结果,所以 UNION ALL
将消除一些多余的 CPU 工作。另外,UNION
可能会导致顺序更改(对重复数据删除进行操作),其中 UNION ALL
不会(在我习惯的 RDBMS 上)。
这里有两种口味供您点餐
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;