PostgreSQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me
Go Back   Dev Shed ForumsDatabasesPostgreSQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old September 17th, 2003, 12:31 PM
oneubauer oneubauer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 oneubauer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Possible to use INNER JOIN with DELETE

Hello all,

Well, the subject says it all. I'd really like to be able to delete records conditionally based on matched values to another table. Viz

DELETE
FROM table_a INNER JOIN table_b" ON table_a.JobNumber = table_b.JobNumber WHERE (table_b.closed Is Not Null) AND (table_a.exported Is True) AND (table_a.JobNumber LIKE '00_____' );

Is this possible with Postgresql v 7.3?

If not, does anyone know of a clever way to get around this limitation?

Thx
o

Reply With Quote
  #2  
Old September 17th, 2003, 03:26 PM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Seems to work for me on an ancient version of PG (7.1.3).

Just tried this query out on one of my DBs as a test

DELETE FROM announcement WHERE announcement.ann_dept = department.dept_no

Very similar to your query, just tack on the additional criteria with ANDs and you should be good.

Let us know if you have any probs.
__________________
PostgreSQL, it's what's for dinner...

Reply With Quote
  #3  
Old September 17th, 2003, 03:47 PM
oneubauer oneubauer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 oneubauer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Hmmm. Not quite.

The table that I am deleting from (call it table_a) has a field "JobNumber" that relates to table_b with a field named "JobNumber". Based on the related information in table_b, namely whether that job is closed or not, I need to delete from table_a.
Your example doesn't quite do that. Your example would delete all records where table_a.JobNumber = table_b.JobNumber.

By definition, *all* records in table_a will match this condition (since all job numbers used in table_a are kept in table_b). I need the query to do an INNER JOIN based on a match and go a step further: test the *related* value in table_b when jobnumber=jobnumber.

The example I give gives syntax errors and I've read somewhere that mySQL didn't do this sort of thing in past versions; I'm thinking postgresql might have a similar limitation.

Of course, I might be completely misunderstanding you......

Reply With Quote
  #4  
Old September 18th, 2003, 10:33 AM
bcyde's Avatar
bcyde bcyde is offline
Me likey breadsticks...
Dev Shed Beginner (1000 - 1499 posts)
 
Join Date: Jan 2003
Location: Los Angeles
Posts: 1,189 bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level)bcyde User rank is Sergeant (500 - 2000 Reputation Level) 
Time spent in forums: 3 Days 1 h 12 m 58 sec
Reputation Power: 12
Send a message via AIM to bcyde Send a message via Yahoo to bcyde
Quote:
Your example would delete all records where table_a.JobNumber = table_b.JobNumber.


Yeah, that's why in my post I said

Quote:
Very similar to your query, just tack on the additional criteria with ANDs and you should be good.


This doesn't work for you?

DELETE
FROM table_a WHERE table_a.JobNumber = table_b.JobNumber AND (table_b.closed IS NOT NULL) AND (table_a.exported IS TRUE) AND (table_a.JobNumber LIKE '00_____' );

What errors do you get if you try that?
-b

Reply With Quote
  #5  
Old September 18th, 2003, 11:06 AM
oneubauer oneubauer is offline
Junior Member
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Sep 2003
Posts: 5 oneubauer User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: < 1 sec
Reputation Power: 0
Ugh. No Errors. That worked beautifully.

This is a classic example of doing something one way for so long that one fails to see how an alternative might work until one is hit over the head with it.

I was so fixated on INNER JOINs I didn't even consider how a WHERE statement would do the same thing, essentially. As I suspected, I completely missed the point of your original post.


Anyway, thank you for the cuff-to-the-head I was in desperate need of.

o

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesPostgreSQL Help > Possible to use INNER JOIN with DELETE


Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump


Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 





© 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway
Stay green...Green IT