在不同的列mysql上删除相同的值行

时间:2012-09-20 20:42:09

标签: php mysql

我遇到了一个非常基本的问题,我想跳过三列重复值的行。

表Feed

id,type,user_id,friend_id,date
1, 'friend', 4 , 5 , 5/5/2010
2, 'friend', 5 , 4 , 5/5/2010

现在这就是数据的保存方式(我无法更改保存模块)

因为两者都有相同的东西,所以我想只选择1行而不是2。 我不想在PHP结束时验证并删除它,如果我在PHP处理,那么分页会被打扰

编辑:

表格结构

create table `feed` (
    `id` double ,
    `type` blob ,
    `user_id` double ,
    `type_id` double ,
    `date` datetime 
); 
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('78','friends','1314','1313','2012-09-03 19:48:14');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('79','friends','1313','1314','2012-09-03 19:48:14');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('80','friends','1314','1312','2012-09-03 19:49:07');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('82','friends','1313','1312','2012-09-03 19:49:09');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('84','friends','1315','1312','2012-09-03 19:49:24');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('86','friends','1315','1313','2012-09-03 19:49:33');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('87','friends','1313','1315','2012-09-03 19:49:33');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('97','friends','1317','1312','2012-09-03 19:55:06');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('99','friends','1313','1317','2012-09-03 19:56:01');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('100','friends','1317','1313','2012-09-03 19:56:01');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('101','friends','1315','1317','2012-09-03 19:56:58');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('102','friends','1317','1315','2012-09-03 19:56:58');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('104','following','1313','1193','2012-09-03 19:59:39');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('105','following','1313','1308','2012-09-03 19:59:51');
insert into `feed` (`id`, `type`, `user_id`, `type_id`, `date`) values('106','following','1313','1098','2012-09-03 19:59:58');

4 个答案:

答案 0 :(得分:1)

您正在寻找的功能是DISTINCT()。这允许您按该列对表进行分组并删除重复项。

只需在select语句中确保您也选择其他字段列:

  

SELECT id,DISTINCT(type),user_idfriend_iddate FROM TABLENAME

答案 1 :(得分:1)

您可以在桌子上执行JOIN,这应该可以满足您的需求。 这样的事情可能会起到作用:

SELECT * FROM tableName a
JOIN tableName b ON a.user_id = b.friend_id

答案 2 :(得分:1)

这是最终的解决方案!如果存在相同的友谊对(反向),则只需使用user_id> friend_id。

SELECT DISTINCT type, user_id, friend_id, date 
FROM table t1 
WHERE t1.user_id > t1.friend_id 
    OR NOT EXISTS (
        SELECT * FROM table t2 
            WHERE t1.type=t2.type AND t1.date=t2.date 
            AND t2.user_id = t1.friend_id AND t2.friend_id = t1.user_id 
    )

答案 3 :(得分:0)

你应该通过运行:

来摆脱那些重复的记录
DELETE FROM table t1 WHERE
  EXISTS (
    SELECT * FROM table t2 
    WHERE t1.type=t2.type AND t1.date=t2.date 
      AND t2.user_id = t1.friend_id AND t2.friend_id = t1.user_id 
      AND t1.user_id > t2.user_id 
  )

然后你可以通过以下方式获得不同的友谊记录:

SELECT DISTINCT type, user_id, friend_id, date FROM table

此外,您可以编写一个在INSERT上触发的数据库触发器,用于检查是否已存在重复(具有反向的user_id和friend_id)。如果存在则执行ROLLBACK,否则允许INSERT。