好吧,我有一个名为 posts 的表,我尝试删除重复项,条件是如果 post_id 之一等于 post_dat_id,并且仅当 post_data_id == post_id 的 post_type 为 2 时才删除它。
帖子结构
post_id || post_data_id || post_type || post_user_id
==================================
1 || NULL || 0 || 10210
2 || 1 || 2 || 201020 <-- it shouldn't show this one because it meets the condition that post_type == 2 && the post_data_id is equal to one of the post_id. EDIT: also notice how this id = 2 but the post_data_id = 1. It is impossible for a row ot have the same post_id & post_data_id
2 || 1 || 0 || 202020
3 || 6 || 2 || 202020
我的mysql:
SELECT p.*
FROM posts p
LEFT JOIN following f ON f.user_id =1
AND p.post_user_id = f.follower_id
WHERE post_user_id =1
OR f.user_id IS NOT NULL
ORDER BY `p`.`post_time` DESC
LIMIT 0 , 10
编辑:我不想删除代码,我想做的只是在满足我选择的条件时不显示结果。另外,我已经在使用左连接,因为我的 sql 代码需要检查下表来获取用户 ID
编辑2:我还将post_data更改为post_data_id,因此它现在是纯int
最佳答案
还没有测试过这段代码,但我会做这样的事情:
select from posts p where p.id not in
(select q.id from posts q
where q.post_data = q.post_id and q.post_type = 2);
关于mysql - 删除 SELECT Mysql 结果中的重复项,如果任何列 1 = 列 2 那么它是重复项,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18111235/