dbTalk Databases Forums  

Delete from a table with JOIN

comp.databases.postgresql.novice comp.databases.postgresql.novice


Discuss Delete from a table with JOIN in the comp.databases.postgresql.novice forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
Noel
 
Posts: n/a

Default Delete from a table with JOIN - 01-12-2004 , 12:37 AM






Hi,

I'm trying to delete from a table based on a foreign key from another table.
I've tried this:
DELETE FROM a
FROM a LEFT JOIN b
ON a.b = b.id
WHERE b.foo = 100

Also tried with the second FROM dropped.
All I get is a parse error:
parser: parse error at or near "JOIN" at character 11, just after first
FROM a.

Any suggestions would be appreciated
Cheers

--
Noel Faux
Department of Biochemistry and Molecular Biology
Monash University
Clayton 3168
Victoria
Australia



---------------------------(end of broadcast)---------------------------
TIP 7: don't forget to increase your free space map settings


Reply With Quote
  #2  
Old   
Tom Lane
 
Posts: n/a

Default Re: Delete from a table with JOIN - 01-12-2004 , 08:35 AM






Noel <noel.faux (AT) med (DOT) monash.edu.au> writes:
Quote:
I've tried this:
DELETE FROM a
FROM a LEFT JOIN b
ON a.b = b.id
WHERE b.foo = 100
There is no such syntax in Postgres (as I would have thought would be
reasonably apparent from the DELETE reference page).

BTW, if you had been able to do the above, you would have quickly
regretted it, since it would delete *every* row in a. Better think
twice about the use of LEFT JOIN here.

You might be able to do what you want with a sub-select in the WHERE
clause, along the lines of

DELETE FROM a WHERE id IN
(SELECT b.id FROM b WHERE b.foo = 100);

This IN syntax is not too efficient in PG releases preceding 7.4,
but should work fine as of 7.4.

regards, tom lane

---------------------------(end of broadcast)---------------------------
TIP 8: explain analyze is your friend



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



Powered by vBulletin Version 3.5.3
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.