L
L
LebedevStr2016-04-03 20:35:42
MySQL
LebedevStr, 2016-04-03 20:35:42

How to remove duplicates in SQL tables?

There is a table with the following content:
cname | cmail
--------------------
Abyz | [email protected]
StroyMast | [email protected]
Clever Shop | [email protected]
Clever Shop 3 | [email protected]
Big Mart 1 | [email protected]
Big Mart 2 | [email protected] It is
necessary to remove duplicates in the cmail column.
Result
cname | cmail
--------------------
Abyz | [email protected]
StroyMast | [email protected]
Clever Shop | [email protected]
Big Mart 1 | [email protected]
Thank you.

Answer the question

In order to leave comments, you need to log in

5 answer(s)
T
ThunderCat, 2016-04-03
@LebedevStr

make the table duplicate and transfer all unique values

INSERT INTO TestTable (cname, cmail)
SELECT cname, cmail
FROM Users
group by cmail

or
SELECT cname, cmail
INTO TestTable 
FROM Users
group by cmail

I
Ivan, 2016-04-03
@0neS

stackoverflow.com/questions/3311903/remove-duplica...

H
hscode, 2016-04-03
@hscode

The easiest option
1. In a loop, go through all the records in the table
2. When reading each record, do a search in the table, is there such a record yet?
3. If there is, then delete it.

M
mletov, 2016-04-03
@mletov

SELECT MIN(cname), cmail
FROM table
GROUP BY cmail

N
nozzy, 2016-04-03
@nozzy

SELECT	
t1.cname, t1.cmail
FROM
(
  SELECT cname, cmail,
       @cmail_rank := IF(@current_cmail = cmail, @cmail_rank + 1, 1) AS cmail_rank,
       @current_cmail := cmail 
  FROM your_table
  ORDER BY cname, cmail
) t1
WHERE t1.cmail_rank = 1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question