我正在使用 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/