IT Community - Software Programming, Web Development and Technical Support

How to remove duplicate records from a table? with out using distinct key?

This is a discussion on How to remove duplicate records from a table? with out using distinct key? within the Database Support forums, part of the Web Development category; How to remove duplicate records from a table? with out using distinct key?...


Go Back   IT Community - Software Programming, Web Development and Technical Support > Web Development > Database Support

Register FAQ Members List Calendar Mark Forums Read
  1 links from elsewhere to this Post. Click to view. #1 (permalink)  
Old 08-06-2007, 06:23 AM
KiruthikaSambandam KiruthikaSambandam is offline
D-Web Analyst
 
Join Date: Aug 2007
Posts: 332
KiruthikaSambandam is on a distinguished road
Default How to remove duplicate records from a table? with out using distinct key?

How to remove duplicate records from a table? with out using distinct key?
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 08-06-2007, 08:04 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: How to remove duplicate records from a table? with out using distinct key?

Hi,

I have used UNION functions to get the distinct result from a table.

Here are the details...

Quote:
mysql> create table test(id integer, name varchar(10));
mysql> insert into test values (1, 'john');
mysql> insert into test values (1, 'john');
mysql> insert into test values (2, 'mike');
mysql> insert into test values (2, 'philip');
mysql> insert into test values (3, 'smith');
mysql> insert into test values (3, 'smith');
mysql> select * from test;
+------+--------+
| id | name |
+------+--------+
| 1 | john |
| 1 | john |
| 2 | mike |
| 2 | philip |
| 3 | smith |
| 3 | smith |
+------+--------+
6 rows in set (0.00 sec)

mysql> select a.id, a.name from test a
-> union
-> select b.id, b.name from test b;

+------+--------+
| id | name |
+------+--------+
| 1 | john |
| 2 | mike |
| 2 | philip |
| 3 | smith |
+------+--------+
4 rows in set (0.00 sec)

mysql> select distinct * from test;
+------+--------+
| id | name |
+------+--------+
| 1 | john |
| 2 | mike |
| 2 | philip |
| 3 | smith |
+------+--------+
4 rows in set (0.00 sec)
I hope it will work !!..
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #3 (permalink)  
Old 08-06-2007, 08:10 AM
kumaresan kumaresan is offline
D-Web Trainee
 
Join Date: Jul 2007
Posts: 12
kumaresan is on a distinguished road
Send a message via AIM to kumaresan Send a message via Yahoo to kumaresan
Default Re: How to remove duplicate records from a table? with out using distinct key?

drop table if exists duplicate;
CREATE TABLE duplicate (
i int,
name
varchar(20)
);
insert into duplicate values(1,'kumar');
insert into duplicate values(1,'kumar');
insert into duplicate values(2,'ram');
insert into duplicate values(2,'ram');
insert into duplicate values(3,'arjun');
insert into duplicate values(3,'arjun');


SELECT * FROM duplicate;

i name
----------------
1 kumar
1 kumar
2 ram
2 ram
3 arjun
3 arjun

to remove the duplicate rows
-------------------------------
alter ignore table duplicate add unique index(i,name);


SELECT * FROM duplicate;

i name
-----------------
1 kumar
2 ram
3 arjun
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #4 (permalink)  
Old 08-06-2007, 08:27 AM
S.Vinothkumar S.Vinothkumar is offline
D-Web Genius
 
Join Date: May 2007
Posts: 1,061
S.Vinothkumar is on a distinguished road
Default Re: How to remove duplicate records from a table? with out using distinct key?

You can see answer here step by step...
__________________
S.VinothkumaR
Behind me is infinite power,
Before me is Endless Possibility,
Around me is Boundless Opportunity,
Why should I fear!
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #5 (permalink)  
Old 08-06-2007, 08:38 AM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Default Re: How to remove duplicate records from a table? with out using distinct key?

Hi,
The below should display the distinct values

SELECT i,name FROM(select i,name,count(*) from duplicate
group by i,name having count(*)>1)t

Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #6 (permalink)  
Old 08-06-2007, 08:55 AM
priyan priyan is offline
D-Web Sr.Programmer
 
Join Date: Mar 2007
Posts: 133
priyan is on a distinguished road
Default Re: How to remove duplicate records from a table? with out using distinct key?

Hi Prasath,

You need to modify your query little bit. If i inserted another record into the duplicate table,

Quote:
insert into duplicate values(4,'raman');

select * from duplicate;
------------------
i name
------------------
1 kumar
1 kumar
2 ram
2 ram
3 arjun
3 arjun
4 raman
------------------

SELECT i,name FROM(select i,name,count(*) from duplicate
group by i,name having count(*)>1)t;

------------------
i name
------------------
1 kumar
2 ram
3 arjun
------------------
-- It's going to be wrong result, now modify the above query as given below.

SELECT i,name FROM(select i,name,count(*) from duplicate
group by i,name having count(*)>=1)t;
------------------
i name
------------------
1 kumar
2 ram
3 arjun
4 raman
------------------
__________________
Keep smiling...
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
  #7 (permalink)  
Old 08-06-2007, 10:16 PM
prasath prasath is offline
D-Web Sr.Programmer
 
Join Date: Jul 2007
Location: Chennai
Posts: 173
prasath is on a distinguished road
Thumbs up Re: How to remove duplicate records from a table? with out using distinct key?

Hi,
We concentrate tedious query,but GROUP BY simply do to remove the
duplicate records.

SELECT i,name from duplicate group by i,name

It is working as fine..........

Thanks,
Prasath.K
Add Post to del.icio.usBookmark Post in TechnoratiFurl this Post!
Reply With Quote
Reply


Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

LinkBacks (?)
LinkBack to this Thread: http://www.discussweb.com/database-support/3153-how-remove-duplicate-records-table-out-using-distinct-key.html
Posted By For Type Date
DiscussWeb IT Community - Fusing This thread Refback 08-06-2007 10:37 PM

Similar Threads
Thread Thread Starter Forum Replies Last Post
Delete records from multiple table at a time Falcon Database Support 7 04-07-2008 01:12 AM
How to get distinct records from datatable oxygen C# Programming 1 03-05-2008 02:47 AM
Duplicate Content vadivelanvaidyanathan Search Engine Optimization 11 02-27-2008 11:10 AM
What is the difference between DELETE TABLE and TRUNCATE TABLE commands in SQL Server oxygen Database Support 6 11-23-2007 06:17 AM
Duplicate elements from an array vigneshgets C and C++ Programming 1 09-06-2007 08:04 AM


All times are GMT -7. The time now is 06:57 AM.


Copyright ©2004 - 2007, DiscussWeb. All Rights Reserved.

SEO by vBSEO 3.0.0