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 > delete rows from tableA that are in tableB

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 12-02-11, 06:48
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
delete rows from tableA that are in tableB

I wont to delete all rows in customer_products that are for customers who are in the customers_archive table but i can't seem to see what i'm doing wrong.

delete from customer_products cp
where cp.customer_id in
(select c.customer_id from customer_archive c)

this gives me an error

although this

select * from customer_products cp
where cp.customer_id in
(select c.customer_id from customer_archive c)

works fine???

any help much appreciated.

ta

jim
Reply With Quote
  #2 (permalink)  
Old 12-02-11, 07:30
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
try this --
Code:
delete from customer_products 
where customer_id in
(select customer_id from customer_archive)
or this --
Code:
delete customer_products 
  from customer_products 
inner
  join customer_archive
    on customer_archive.customer_id = customer_products.customer_id
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #3 (permalink)  
Old 12-02-11, 07:46
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
thanks rudy, that first one works fine... how come it doesn't like the aliases?
Reply With Quote
  #4 (permalink)  
Old 12-02-11, 07:58
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,525
Quote:
Originally Posted by jx12345 View Post
how come it doesn't like the aliases?
not allowed in the syntax, i guess

you might find the joined delete faster than the IN subquery

admittedly not a big deal if you need to do this only once
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #5 (permalink)  
Old 12-02-11, 08:15
jx12345 jx12345 is offline
Registered User
 
Join Date: Apr 2010
Posts: 51
the join looks like voodoo to me i'm too scared to use it.
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