dbTalk Databases Forums  

query help.. delete with sub select maybe ?

comp.databases.mysql comp.databases.mysql


Discuss query help.. delete with sub select maybe ? in the comp.databases.mysql forum.



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

Default query help.. delete with sub select maybe ? - 09-23-2010 , 10:47 AM






Really not sure how to do this.. but I suspect a sub select is needed

I have 2 tables, both have a column labeled ID , I need to delete the
row in tableONE whenever a matching ID was NOT found in tableTWO ..
Sounds simple.. and probably is, I'm just not finding the right
incantation.

Reply With Quote
  #2  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: query help.. delete with sub select maybe ? - 09-23-2010 , 11:02 AM






On 23 Sep, 16:47, dewed <duane.lor... (AT) gmail (DOT) com> wrote:
Quote:
Really not sure how to do this.. but I suspect a sub select is needed

I have 2 tables, both have a column labeled ID , I need to delete the
row in tableONE whenever a matching ID was NOT found in tableTWO *..
Sounds simple.. and probably is, I'm just not finding the right
incantation.
You want a LEFT JOIN on the ID with the WHERE clause specifying
WHERE tableTWO.ID IS NULL

Reply With Quote
  #3  
Old   
dewed
 
Posts: n/a

Default Re: query help.. delete with sub select maybe ? - 09-23-2010 , 11:46 AM



Thanks for the cluestick ..

Here is what I came up with . . . testing as a select first, just to
be safe

select tableONE.ID , tableTWO.ID
FROM tableONE
LEFT JOIN tableTWO ON tableONE.ID = tableTWO.ID
WHERE tableTWO.ID IS NULL


The query appears to work as intended, but currently finds no matches,
which is an unexpected result. I would of thought there would be at
least a few matches, so I'd apreciate if you could let me know if the
query above looks correct to you.. or do you need a bigger cluestick :P

Reply With Quote
  #4  
Old   
enos76
 
Posts: n/a

Default Re: query help.. delete with sub select maybe ? - 09-23-2010 , 01:14 PM



dewed wrote: [...]

Quote:
SELECT tableONE.ID, tableTWO.ID
FROM tableONE
LEFT JOIN tableTWO ON tableONE.ID = tableTWO.ID
WHERE tableTWO.ID IS NULL

Looks fine to me.

Here I have a similar scenario, with the tables "orders" and orderRows",
both with column orderID: your sintax does find the orders which are not
referenced by any orderRow. In my case, orderID is an external key of
table orderRows, but I do not think that it affects the result.
I have then used the following sintax to delete the orders:

DELETE tableONE
FROM tableONE
LEFT JOIN tableTWO ON tableONE.ID = tableTWO.ID
WHERE tableTWO.ID IS NULL;


Regards
--
Enos

Reply With Quote
  #5  
Old   
Captain Paralytic
 
Posts: n/a

Default Re: query help.. delete with sub select maybe ? - 09-24-2010 , 03:43 AM



On 23 Sep, 17:46, dewed <duane.lor... (AT) gmail (DOT) com> wrote:
Quote:
Thanks for the cluestick ..

Here is what I came up with *. . . *testing as a select first, just to
be safe

select tableONE.ID , *tableTWO.ID
FROM *tableONE
LEFT JOIN *tableTWO ON tableONE.ID *= *tableTWO.ID
WHERE tableTWO.ID IS NULL

The query appears to work as intended, but currently finds no matches,
which is an unexpected result. *I would of thought there would be at
least a few matches, so I'd apreciate if you could let me know if the
query above looks correct to you.. or do you need a bigger cluestick :P
That looks perfectly correct. Why not add a dummy record to tableONE
only and run the query again to check that that record is indeed
returned?

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.