3

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

表格提要

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

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

因为两者都有相同的东西,所以我只想选择它们作为 1 行而不是 2。我不想在 PHP 端验证和删除它,b/c 如果我在 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

4 回答 4

1

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

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

SELECT id, DISTINCT( type), user_id, friend_id,date从表名

于 2012-09-20T20:45:37.983 回答
1

您可以JOIN在桌子上执行 a ,这应该可以满足您的需求。这样的事情可能会奏效:

SELECT * FROM tableName a
JOIN tableName b ON a.user_id = b.friend_id
于 2012-09-20T20:51:03.640 回答
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 
    )
于 2012-09-20T21:26:20.487 回答
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。

于 2012-09-20T21:20:03.107 回答