将 DISTINCT 添加到 UNION 查询

发布于 2024-11-12 05:56:11 字数 239 浏览 3 评论 0原文

我如何从中获得不同的 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

旧竹 2024-11-19 05:56:11

只需删除全部即可。有些风格允许添加 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 adding DISTINCT instead of ALL 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.

囚你心 2024-11-19 05:56:11

简单的方法不是完全去掉并集和第二部分吗:

SELECT Title.id, Title.title FROM titles as Title HAVING points > 0 

因为 HAVINGpoints> 0 包括具有 HAVING 点 > 的任何内容1?

Isn't the simple way just get rid of the union and the second part altogether:

SELECT Title.id, Title.title FROM titles as Title HAVING points > 0 

since HAVING points > 0 includes anything with HAVING points > 1?

左秋 2024-11-19 05:56:11

您可以按照其他人的建议删除ALL

如果两个查询使用相同的表并且仅在 WHERE 子句或仅在 HAVING 子句中不同,您还可以使用以下命令:

SELECT Title.id, Title.title FROM titles as Title
WHERE (1st query conditions)
   OR (2nd query conditions)

SELECT Title.id, Title.title FROM titles as Title
HAVING (1st query conditions)
    OR (2nd query conditions)

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 the HAVING clause, you can also use this:

SELECT Title.id, Title.title FROM titles as Title
WHERE (1st query conditions)
   OR (2nd query conditions)

or

SELECT Title.id, Title.title FROM titles as Title
HAVING (1st query conditions)
    OR (2nd query conditions)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文