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 > how can I delete duplicate regists

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-12-08, 11:00
pfonseka pfonseka is offline
Registered User
 
Join Date: Jun 2008
Posts: 3
how can I delete duplicate regists

i'm trying to delete duplicate regists with this code, but I'vve got this error:
"You can't specify target table 'newsletters_queu' for update in FROM clause "

the code retrives the data I want to delete but doesn't allow me to do it.

DELETE FROM newsletters_queu WHERE (
( (
newsletters_queu.id
) NOT IN (
SELECT min( newsletters_queu.id )
FROM newsletters_queu
GROUP BY newsletters_queu.idnewsletter, newsletters_queu.idendereco
) )
)
Reply With Quote
  #2 (permalink)  
Old 06-12-08, 11:15
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
You could either put the value into a variable if you're using a stored proc
Code:
declare v_id int;

SELECT min( newsletters_queu.id ) 
into v_id
FROM newsletters_queu;

DELETE FROM newsletters_queu 
WHERE newsletters_queu.id != v_id;
or into a temporary table a bit like this
Code:
SELECT min( newsletters_queu.id ) as id
into tmp_tab
FROM newsletters_queu;

DELETE FROM newsletters_queu 
WHERE newsletters_queu.id not in ( select id from tmp_tab);

drop table tmp_tab;
Mike
Reply With Quote
  #3 (permalink)  
Old 06-12-08, 12:06
pfonseka pfonseka is offline
Registered User
 
Join Date: Jun 2008
Posts: 3
I'm getting this error : "Undeclared variable: tmp_tab"

when I execute:

SELECT min( newsletters_queu.id ) as id
into tmp_tab
FROM newsletters_queu;
Reply With Quote
  #4 (permalink)  
Old 06-12-08, 12:19
mike_bike_kite mike_bike_kite is offline
vaguely human
 
Join Date: Jun 2007
Location: London
Posts: 2,519
Sorry - I've been using Sybase all day!

Code:
-- do this once
create table tmp_id (
   id   int
);

-- then do this each time you want to do the delete
delete from tmp_id;

insert tmp_id
SELECT min( id )
FROM newsletters_queu;

DELETE FROM newsletters_queu 
WHERE id not in ( select id from tmp_tab);
Reply With Quote
  #5 (permalink)  
Old 06-12-08, 12:53
pfonseka pfonseka is offline
Registered User
 
Join Date: Jun 2008
Posts: 3
thanks you for your help... its working:

CREATE TABLE TMP_id(id int);

INSERT TMP_id SELECT min( newsletters_queu.id ) FROM newsletters_queu GROUP BY newsletters_queu.idnewsletter, newsletters_queu.idendereco;

DELETE FROM newsletters_queu WHERE id not in ( select id from TMP_id)

DROP TABLE TMP_id;
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