gpt4 book ai didi

mysql - 删除重复项 UNION ALL 检索到的帖子 sql, mysql

转载 作者:太空宇宙 更新时间:2023-11-03 10:34:36 25 4
gpt4 key购买 nike

我正在使用 UNION ALL 子句来检索用户的帖子。

SELECT * FROM $post WHERE condition 1

UNION ALL

SELECT * FROM $post WHERE condition 2

结果

Post 1
Post 2
Post 3
Post 3
Post 5

预期

Post 1
Post 2
Post 3 (display only once)
Post 5

问题

在使用 UNION ALL 时,如何只显示一次重复的帖子

最佳答案

使用UNION DISTINCT,或者只是UNION

https://dev.mysql.com/doc/refman/5.7/en/union.html说:

The default behavior for UNION is that duplicate rows are removed from the result. The optional DISTINCT keyword has no effect other than the default because it also specifies duplicate-row removal.

如果您的查询必须使用UNION ALL,则将其设为子查询并使用:

SELECT DISTINCT ... FROM ( ...subquery... ) AS t

关于mysql - 删除重复项 UNION ALL 检索到的帖子 sql, mysql,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50827639/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com