mysql - 删除 SELECT Mysql 结果中的重复项,如果任何列 1 = 列 2 那么它是重复项

标签 mysql

好吧,我有一个名为 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/

相关文章:

php - Jquery AJAX 未在 PHP 上传递 “POST” 数据

mysql - MySQL 上的 LIKE 查询有多慢? (自定义字段相关)

php - 如果数据库中已存在值,如何显示错误消息?

Mysql查询不同的group by

php - mysql 基于 AND 搜索行

mysql - 连接 3 个表 - MySQL

mysql - 如何获取每次更新查询中mysql受影响的行数

php - 如何检查注册页面中给定的用户名是否存在于两个表中的某些中

mysql - 一对多与多对多关联设计和优势

mysql - 如何选择另一列的所有值都等于某值的列值?