If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

 
Go Back  dBforums > Database Server Software > MySQL > dealing with double records

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 02-15-05, 10:48
gtk gtk is offline
Registered User
 
Join Date: Oct 2004
Location: Paris, FRANCE
Posts: 132
Post dealing with double records

I have a table on which I forgot to put an index key combination.
This is a relation table like that:
Code:
relation_id | user_id | product_id
I'd like to remove doubloons between user_id and product_id.
A user can't be associated multiple times with the same product.

Is it possible to remove them with a query ?
Reply With Quote
  #2 (permalink)  
Old 02-15-05, 10:56
r937 r937 is online now
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
longterm, your strategy should be to remove relation_id and make the other two a composite primary key

for now, you will want to run this --
Code:
create table keepers
select min(relation_id) as min_rel
     , user_id 
     , product_id
  from yourtable
group
    by user_id 
     , product_id
having count(*) > 1     
;     
delete yourtable
  from yourtable
     , keepers
 where yourtable.user_id = keepers.user_id
   and yourtable.product_id = keepers.product_id
   and yourtable.relation_id > keepers.min_rel
;
as always, before you run any sql that changes data, it's a good idea to take a backup first
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 02-15-05, 22:40
SKJoy SKJoy is offline
Registered User
 
Join Date: Feb 2004
Posts: 13
If u create temporary tables to remove doubloons, then the issue of transaction & synchronization check comes in that leads to a better level of difficulty. A single line dynamic SQL should be the best choice that bypasses risks & does exactly what we need like bullet. Again, a single line SQL should be a better choice for cross database compatibility.
__________________
Shahriar Kabir
Reply With Quote
Reply

Thread Tools Search this Thread
Search this Thread:

Advanced Search
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

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