T
T
TKiTANO2015-12-26 08:16:26
MySQL
TKiTANO, 2015-12-26 08:16:26

How to remove the lines in which there is a repetition?

We have a table with the following data in it:
id | id2 | id3
1 28 1008
2 28 1009
3 13 1005
4 2 1001
5 28 1010
6 28 1008
7 28 1010
.....
i.e. in this case, you need to delete id 6 and 7. And id 1 and 5 should be left. It turns out that the rows that were added last with id2 = 28 and a repeating id3 should be deleted.
How can this be done?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry Bay, 2015-12-26
@kawabanga

if through mysql, then something like this, just be sure to check what gives such a request, since I just sketched out the code.
For the future:
- name the names of the columns clearly
- add a key to the two unique fields.

R
Rsa97, 2015-12-26
@Rsa97

Well, the fact that the id of one entry is less than that of the other does not mean that this entry was made earlier. And the task is so easy

DELETE `t1`.*
    FROM `table` AS `t1`
    LEFT JOIN `table` AS `t2` ON `t2`.`id` < `t1`.`id` 
        AND `t2`.`id2` = `t1`.`id2` AND `t2`.`id3` = `t1`.`id3`
    WHERE `t2`.`id` IS NOT NULL

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question