|
-
Feb 21st, 2009, 08:58 AM
#1
Thread Starter
Fanatic Member
Delete with join query
I have a query I use to return rows from two tables with kriteries. But how can I convert this query to a delete query?
I wanna delete the result of this query on both tables.
I have try to replce SELECT tblCust.*, tblCustDesc.* with Delete tblCust, tblCustDesc without any luck.
Code:
SELECT tblCust.*, tblCustDesc.*
FROM tblCust INNER JOIN tblCustDesc ON tblCust.ID = tblCustDesc.Order_ID
WHERE tblCustDesc.State_ID In (SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
AND tblCust.State_ID In (SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
AND tblCust.StartedBy Is Null
-
Feb 21st, 2009, 09:05 AM
#2
Re: Delete with join query
You need to delete each table individually. You can't delete two tables in one statement.
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Feb 21st, 2009, 09:12 AM
#3
Thread Starter
Fanatic Member
Re: Delete with join query
Ok, thanks for the answer. I believe it was possible.
So, If I should do this this individually, I must start with table tblCustDesc. How should the queries look lik then? Like this? Or do you have any other better solutions?
Code:
DELETE FROM tblCustDesc
WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
And Order_ID IN ( SELECT tblCust.ID FROM tblCust WHERE tblCust.StartedBy Is Null)
And Order_ID IN ( SELECT tblCust.ID FROM tblCust WHERE tblCust.state_ID =
( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description = 'OK');
'Execute the query
DELETE FROM tblCust
WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
And ID Not IN ( SELECT tblCustDesc.Order_ID FROM tblCustDesc WHERE state_ID IN
( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description <> 'OK')
And tblCust.StartedBy Is Null;
'Execute the query
Last edited by Pirre001; Feb 21st, 2009 at 09:19 AM.
-
Feb 21st, 2009, 09:25 AM
#4
Re: Delete with join query
Last one is missing a close ) before the last And
Sometimes the Programmer
Sometimes the DBA
Mazz1
-
Feb 21st, 2009, 09:43 AM
#5
Thread Starter
Fanatic Member
Re: Delete with join query
OK, so this is the right way to do the delete query?
Code:
DELETE FROM tblCustDesc
WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
And Order_ID IN ( SELECT tblCust.ID FROM tblCust WHERE tblCust.StartedBy Is Null)
And Order_ID IN ( SELECT tblCust.ID FROM tblCust WHERE tblCust.state_ID =
( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description = 'OK'));
'Execute the query
DELETE FROM tblCust
WHERE state_ID IN ( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description='OK')
And ID Not IN ( SELECT tblCustDesc.Order_ID FROM tblCustDesc WHERE state_ID IN
( SELECT StateDef.ID FROM StateDef WHERE StateDef.Description <> 'OK'))
And tblCust.StartedBy Is Null;
'Execute the query
-
Feb 21st, 2009, 10:02 AM
#6
Re: Delete with join query
Sometimes the Programmer
Sometimes the DBA
Mazz1
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|