I have a table like that:
table1
+----+------+--------------+
| id | name | phone_number |
+----+------+--------------+
| 1 | A | 111111111111 |
+----+------+--------------+
| 2 | B | 222222222222 |
+----+------+--------------+
| 3 | C | 222222222222 |
+----+------+--------------+
| 4 | D | 111111111111 |
+----+------+--------------+
| 5 | E | 222222222222 |
+----+------+--------------+
I want to keep only 1 of the same phone number, don't care name. So, the table should be:
table1
+----+------+--------------+
| id | name | phone_number |
+----+------+--------------+
| 1 | A | 111111111111 |
+----+------+--------------+
| 2 | C | 222222222222 |
+----+------+--------------+
I tried many ways, many hrs to delete or select but doesn't work, I did use code to resolve this, but I will appreciate for any explanation, thank you.
DELETE FROM table1
where id NOT IN (SELECT id FROM table1 group by phone)
always said: You can't specify target table 'table1' for update in FROM clause
SELECT *
FROM table1
where id NOT IN (SELECT id FROM table1 group by phone)
always return an empty result.
Best Answer
Given this sample data:
first you determine which rows you want to keep.
With this query you can check which rows will be deleted:
The
delete
query:Another feasible approach would be this: