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 From Select

Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1 (permalink)  
Old 06-11-07, 03:09
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Delete From Select

Hi

I there a way to delete like this ?

Code:
DELETE * FROM `products` WHERE `products_id` IN (
   SELECT p.`products_id`
   FROM `products` p, `products_suppliers` ps
   WHERE p.`products_id` = ps.`products_id`
   AND (p.`products_id` <> 215 AND p.`products_id` <> 305)
)
?

Thanks
__________________
MySQL 5.1
Reply With Quote
  #2 (permalink)  
Old 06-11-07, 04:48
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Yes there is a way to delete like that...
Reply With Quote
  #3 (permalink)  
Old 06-11-07, 04:49
aschk aschk is offline
Registered User
 
Join Date: Mar 2007
Location: 636f6d7075746572
Posts: 770
Incidently this part of your query :

Code:
AND (p.`products_id` <> 215 AND p.`products_id` <> 305)
Will ALWAYS be true AND will still allow product ids 215 and 305 to pass through...
Reply With Quote
  #4 (permalink)  
Old 06-11-07, 07:07
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Code:
   SELECT p.`products_id`
   FROM `products` p, `products_suppliers` ps
   WHERE p.`products_id` = ps.`products_id`
   AND (p.`products_id` <> 215 AND p.`products_id` <> 305)
return all ids except 215 and 305. The DELETE part should delete the rest keeping 215 and 305 in the table.

I seem to get this err-msg
#1093 - You can't specify target table 'table-name' for update in FROM clause
__________________
MySQL 5.1

Last edited by anjanesh; 06-11-07 at 07:16.
Reply With Quote
  #5 (permalink)  
Old 06-11-07, 07:36
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by aschk
...Will ALWAYS be true
i don't think that's right

it would be, if it had said
Code:
AND (p.`products_id` <> 215 OR p.`products_id` <> 305)
with OR, everything is going to be not equal to one or the other

but with AND, i believe it works as intended



anjanesh, would you mind saying in words what you're trying to do?

if i interpret your first query, it says "delete all products that have a supplier and that aren't 215 or 305"

do you have products without a supplier? and you want to keep those? if so, why?
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #6 (permalink)  
Old 06-11-07, 07:40
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
I think aschk is getting at the fact that one product has only one product_id. There is no product with both 215 AND 305 as their id.
Code:
p.`products_id` <> 215 AND p.`products_id` <> 305
I think it should look more like this - note the use of joins and the OR operator
Code:
SELECT	p.products_id
FROM	products p
LEFT JOIN products_suppliers ps
	ON p.products_id = ps.products_id
WHERE	p.products_id <> 215
OR	p.products_id <> 305
To complete your delete statement
Code:
DELETE FROM products
WHERE	product_id IN
	(
	SELECT	p.products_id
	FROM	products p
	LEFT JOIN products_suppliers ps
		ON p.products_id = ps.products_id
	WHERE	p.products_id <> 215
	OR	p.products_id <> 305
	)
HTH ~George
__________________
George
Twitter | Blog
Reply With Quote
  #7 (permalink)  
Old 06-11-07, 07:55
r937 r937 is offline
SQL Consultant
 
Join Date: Apr 2002
Location: Toronto, Canada
Posts: 19,534
Quote:
Originally Posted by georgev
There is no product with both 215 AND 305 as their id.
well, of course

but i fear you have fallen into the same trap

this --

... p.`products_id` <> 215 AND p.`products_id` <> 305

is perfectly okay

whereas what you wrote --

... p.`products_id` <> 215 OR p.`products_id` <> 305

will accept every single row in the table!!!
__________________
r937.com | rudy.ca
please visit Simply SQL and buy my book
Reply With Quote
  #8 (permalink)  
Old 06-11-07, 08:28
gvee gvee is offline
www.gvee.co.uk
 
Join Date: Jan 2007
Location: UK
Posts: 10,156
That's a might big hole I fell in back there!
To remove confusion completely
Code:
WHERE p.products_id NOT IN (215,305)
__________________
George
Twitter | Blog
Reply With Quote
  #9 (permalink)  
Old 06-12-07, 13:20
anjanesh anjanesh is offline
Registered User
 
Join Date: Feb 2005
Location: Mumbai, India
Posts: 161
Quote:
if i interpret your first query, it says "delete all products that have a supplier and that aren't 215 or 305"
Correct. Actually delete all products whose ids arent 215 or 305 and supplier_id is 1. I forgot to mention AND supplier_id = 1 but thats fine.

Quote:
do you have products without a supplier? and you want to keep those? if so, why?
No - all products are assigned a supplier. I dont want to keep the deleted product_ids in the other tables - I'll do the manual cascading later - just wanted to know if I could delete these ids using SQL - without using a server-side language to iterate through.

So is there any way to delete this way ?
Code:
DELETE FROM `table1` WHERE `id` IN
 (SELECT `id` FROM `table1` WHERE `id` <> 215 AND `id` <> 305)
which'll delete all ids except 215 and 305
__________________
MySQL 5.1
Reply With Quote
  #10 (permalink)  
Old 06-13-07, 01:25
shammat shammat is offline
Registered User
 
Join Date: Nov 2003
Posts: 2,408
Quote:
Originally Posted by anjanesh
So is there any way to delete this way ?
Code:
DELETE FROM `table1` WHERE `id` IN
 (SELECT `id` FROM `table1` WHERE `id` <> 215 AND `id` <> 305)
which'll delete all ids except 215 and 305
Why not:
Code:
DELETE FROM table1 WHERE id NOT IN (215,305)
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