开发者

How can I delete duplicate mysql column entries?

开发者 https://www.devze.com 2023-01-19 22:27 出处:网络
I have a mysql table named \"dev\" with a column of \"email\"s.I want to select out all the unique e-mail addresses and delete the duplicates.

I have a mysql table named "dev" with a column of "email"s. I want to select out all the unique e-mail addresses and delete the duplicates.

My structure is as follows:

id    ||    email
1           user1@example.com
2           userB@example.com
3           user1@example.com

What I want is...

id    ||    email
1           user1@example.com
2           userB@examp开发者_如何学JAVAle.com

What mysql query can I used to accomplish this?

Thanks in advance!


Create a new table and select the elements you want to keep into the new table. If you don't need to retain the original ids then you can do this:

INSERT INTO dev2 (email)
SELECT DISTINCT(email)
FROM dev

If you need to retain the id of the first row for each the email address then use this instead:

INSERT INTO dev2 (id, email)
SELECT MIN(id), email
FROM dev
GROUP BY email

Once you have filled the new table you can drop the original table then rename the new table to the correct name.

0

精彩评论

暂无评论...
验证码 换一张
取 消