3

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');
4
  • do you want to eliminate same friendship records? (like in given records "5 is friend of 4" means "4 is friend of 5") Commented Sep 20, 2012 at 20:52
  • yes I want to eliminate the same friendship values, b/c 4->5 and 5->4 are same Commented Sep 20, 2012 at 20:57
  • If an answer solved your problem, please mark it as accepted by clicking the green button. Commented Sep 20, 2012 at 21:06
  • @Zar Yes will do that, but let me try that in my case Commented Sep 20, 2012 at 21:11

4 Answers 4

1

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 
    )
Sign up to request clarification or add additional context in comments.

Comments

1

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

4 Comments

Taking his example, user_id aren't equal in both rows, hence they won't get "merged" with your code as he wants?
No this distinct doesn't work in this case , see this when I run query screencast.com/t/eYm3JWNrXPCp
@Zar, it should grab 1 of the user_ids.
@MajinVegeta: Hm... you might need to add GROUP BY type between the WHERE and ORDER clause.
1

You could perform a JOIN on the table itself, which should do what you are looking for. Something like this might do the trick:

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

1 Comment

won't work. it does not even take distinct type and date columns.
0

You should get rid of those duplicate records by running:

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 
  )

Then you can get distinct friendship records with:

SELECT DISTINCT type, user_id, friend_id, date FROM table

Also, you can write a DB trigger that fires on INSERT, that checks if a duplicate (with reversed user_id and friend_id) already exists. If exists it does ROLLBACK, else allow INSERT.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.