Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove same values rows over different column mysql

Tags:

php

mysql

I 'm stuck on a very basic problem, I want to skip the row which has duplicate values over three columns.

Table feeds

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

Now this is how the data is saved (I can't change the saving module)

since both have same thing, so I want to pick them only as a 1 row not 2. I don't want to validate and remove it at PHP end, b/c if I'll do at PHP the pagination would be disturb

Edit:

Table Structure

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');
like image 625
MZH Avatar asked Jun 12 '26 13:06

MZH


2 Answers

And here is the ultimate solution! If the same friendship pair (reversed) exists it only takes the one where 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 
    )
like image 128
mostar Avatar answered Jun 14 '26 04:06

mostar


The function you are looking for is DISTINCT(). That allows you to group the table by that column and remove duplicates.

Just make sure in your select statement that you also select the other field columns as well:

SELECT id, DISTINCT(type), user_id, friend_id, date FROM TABLENAME

like image 20
Ricardo C Avatar answered Jun 14 '26 05:06

Ricardo C



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!