1

I have a table like

table name : emailvalue

--------------------------------------
| email                    |   value |
-----------------------------------
|  ab@gmail.com            |  A      |
--------------------------------------
|  ab@gmail.com            |  B      |
--------------------------------------
|  ab@gmail.com            |  C      |
--------------------------------------
|  cccc@gmail.com          |  F      |
--------------------------------------
|  dd@gmail.com            |  G      |
--------------------------------------
|  dd@gmail.com            |  A      |
--------------------------------------
|  dd@gmail.com            |  H      |
--------------------------------------

I want to delete all for same email without first row. how i will right the sql in my sql

the SQL should delete this 2 row in mysql

--------------------------------------
|  ab@gmail.com            |  B      |
--------------------------------------
|  ab@gmail.com            |  C      |
--------------------------------------   

 and also        

 --------------------------------------
|  dd@gmail.com            |  A      |
 --------------------------------------
|  dd@gmail.com            |  H      |
--------------------------------------
Sarwar Hasan
  • 1,561
  • 2
  • 17
  • 25
  • There is no such thing as a *first row* in relational databases. Tabales are *multisets* (somtimes called *bags*) of records, not *lists* of records. Keeping this in mind, please clarify what you mean by *first row*. – Oswald Sep 22 '13 at 06:37
  • See the table there are 3 row for email ab@gmail.com . i want to delete last 2 row of those 3 row.. – Sarwar Hasan Sep 22 '13 at 06:40
  • I told you there is no such thing as a *first row*. Likewise, there is no such thing as *last 2 rows*. You have to find some other criterion on which to base the descision about which records to remove. – Oswald Sep 22 '13 at 06:42
  • How do you build that order of our table? – juergen d Sep 22 '13 at 06:43

3 Answers3

3

Use it:

DELETE t1 FROM emailvalue t1
LEFT JOIN 
(
  SELECT email, value
  FROM emailvalue
  GROUP BY email
) t2 on t2.email = t1.email AND t2.value = t1.value
WHERE t2.value is null;
MD SHAHIDUL ISLAM
  • 14,325
  • 6
  • 82
  • 89
2
delete t
from your_table t
left join 
(
  select email, min(value) as minv
  from your_table
  group by email
) x on x.email = t.email and x.minv = t.value
where x.minv is null
juergen d
  • 201,996
  • 37
  • 293
  • 362
1
ALTER IGNORE TABLE `emailvalue` ADD UNIQUE INDEX(`email`);

MySQL remove duplicates from big database quick

MySQL: ALTER IGNORE TABLE ADD UNIQUE, what will be truncated?

Community
  • 1
  • 1
Ning
  • 76
  • 3