473,378 Members | 1,447 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

how to delete duplicate rows and keep the origianal rows in mysql

Hi,


how can i delete the duplicate rows and keep the original row in the mysql date.

rs = "select count(email) as mail from import_address_book group by email having mail > 1
rs.each do |mail|
`delete from import_address_book where email limit #{mail - 1}";
end..


please can any correct it and send me the executing query...
Sep 20 '11 #1

✓ answered by dlite922

once you run this query and get the results into $rs:

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(email) as count, email from import_address_book GROUP BY email HAVING COUNT(email) > 1;
  2.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. while(($row = mysql_fetch_assoc($rs))) { 
  3.  
  4.     mysql_query('DELETE FROM import_address_book WHERE email = '.$row['email'].' ORDER BY id_or_date_column DESC LIMIT '.$row['count']-1;
  5.  
  6. }
  7.  
  8.  
That should do it. I have not tested it though.


Dan

4 2054
Rabbit
12,516 Expert Mod 8TB
Without the table structure, there's not much we can help with.
Sep 20 '11 #2
dlite922
1,584 Expert 1GB
once you run this query and get the results into $rs:

Expand|Select|Wrap|Line Numbers
  1. SELECT COUNT(email) as count, email from import_address_book GROUP BY email HAVING COUNT(email) > 1;
  2.  
Expand|Select|Wrap|Line Numbers
  1.  
  2. while(($row = mysql_fetch_assoc($rs))) { 
  3.  
  4.     mysql_query('DELETE FROM import_address_book WHERE email = '.$row['email'].' ORDER BY id_or_date_column DESC LIMIT '.$row['count']-1;
  5.  
  6. }
  7.  
  8.  
That should do it. I have not tested it though.


Dan
Sep 20 '11 #3
Hi,


thanks for helping me. i got the solution.

I altered this query it is working fine.

Thanks for u r help
Sep 21 '11 #4
dlite922
1,584 Expert 1GB
Can you post how you altered it so that anybody in the same situation can solve it as well?

Thanks,

Dan
Sep 22 '11 #5

Sign in to post your reply or Sign up for a free account.

Similar topics

3
by: hshen | last post by:
Hi All, I have encountered a weird behavoir of embedded MySQL. Through a simple program I made (in Delphi 6), I can insert rows to a table through embedded MySQL and I can retrieve the rows from...
2
by: ms | last post by:
Access 2000: I am trying to delete duplicate records imported to a staging table leaving one of the duplicates to be imported into the live table. A unique record is based on a composite key of 3...
1
by: Charles | last post by:
Hi MS Access & MS SQL Server Gurus ! I am trying to delete duplicate data from a LINKED SQL Server 2000 table (LinkedTable) and insert the result into a local (in Access) table. The following...
4
by: KT | last post by:
Is there any one click solution that would do the trick? I would like to create a button, so the person who maintains the database can perform clean up work to delete duplicate records which...
3
by: kunal4sd | last post by:
how can i delete duplicate entry except one from a table in mysql. please send that query to me. thanks kunal
16
by: Theodore70 | last post by:
I am trying to delete duplicate records in an access table using VB. Not a programmer here, but this was one of the codes that I found on the web. I made some modifications to it, but it still did...
3
by: amitmant | last post by:
HI EveryOne I am facing one prblem that is : How can be delete duplicate records from table(Database) using query and Sub Query. If any one knows then PLz Forward me at amit_mant@rediffmail.com
3
by: rajeshkrsingh | last post by:
Hi friends, Step1- create table duplicate ( intId int, varName varchar(50) ) insert into duplicate(intId,varName) values(1,'rajesh') insert into duplicate(intId,varName) values(2,'raj12')...
6
by: Dilip1983 | last post by:
Hi All, I want to delete duplicate records from a large table. There is one index(INDEX_U1) on 4 columns(col1,col2,col3,col4) which is in unusable state. First of all when i tried to rebuild...
1
AdusumalliGopikumar
by: AdusumalliGopikumar | last post by:
Please can anyone tell me how to delete duplicate rows from a table without using the pseudo column ROWID in the query Thanks in Advance
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.