将 DISTINCT 添加到 UNION 查询
我如何从中获得不同的 title.id:
SELECT Title.id, Title.title FROM titles as Title HAVING points > 0
UNION ALL
SELECT Title.id, Title.title FROM titles as Title HAVING points > 1
查询还有更多内容,但这应该足以继续下去。
How do I get distinct title.id's from this:
SELECT Title.id, Title.title FROM titles as Title HAVING points > 0
UNION ALL
SELECT Title.id, Title.title FROM titles as Title HAVING points > 1
There is more to the query but this should be enough to go on.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
只需删除
全部
即可。有些风格允许添加DISTINCT
而不是ALL
来更加明确,但这是多余的,因为默认总是过滤重复项。MySQL - http://dev.mysql.com/doc/refman/ 5.0/en/union.html
MSSQL - http://msdn.microsoft.com/en-us/library/ ms180026.aspx
ORACLE - https://docs.oracle.com/cd/ B28359_01/server.111/b28286/queries004.htm
PostgreSQL - http://www.postgresql.org/docs/8.3/ Interactive/queries-union.html
ETC。
Just remove the
ALL
. Some flavors allow addingDISTINCT
instead ofALL
to be more explicit, but that's redundant having that the default is always to filter our duplicates.MySQL - http://dev.mysql.com/doc/refman/5.0/en/union.html
MSSQL - http://msdn.microsoft.com/en-us/library/ms180026.aspx
ORACLE - https://docs.oracle.com/cd/B28359_01/server.111/b28286/queries004.htm
PostgreSQL - http://www.postgresql.org/docs/8.3/interactive/queries-union.html
etc.
简单的方法不是完全去掉并集和第二部分吗:
因为
HAVINGpoints> 0
包括具有HAVING 点 > 的任何内容1?
Isn't the simple way just get rid of the union and the second part altogether:
since
HAVING points > 0
includes anything withHAVING points > 1
?您可以按照其他人的建议删除
ALL
。如果两个查询使用相同的表并且仅在
WHERE
子句或仅在HAVING
子句中不同,您还可以使用以下命令:或
You could drop the
ALL
as others have suggested.If the two queries use same tables and are different only in the
WHERE
clause or only in theHAVING
clause, you can also use this:or