To remove duplicate data or rows from a MySQL table, you can use the DISTINCT
keyword in a SELECT
statement.
For example, suppose you have a table named users
with the following data:
id | name | |
---|---|---|
1 | Alice | alice@example.com |
2 | Bob | bob@example.com |
3 | Alice | alice@example.com |
SELECT DISTINCT * FROM users;
This will return the following result:
id | name | |
---|---|---|
1 | Alice | alice@example.com |
2 | Bob | bob@example.com |
SELECT DISTINCT name FROM users;
This will return the following result:
name |
---|
Alice |
Bob |
DELETE FROM users WHERE id NOT IN (SELECT MIN(id) FROM users GROUP BY name, email);
This will delete all rows from the users
table except for the rows with the minimum id
.