tags:

views:

58

answers:

1

Hello all,
I'm working with three tables, and for simplicity's sake let's call them table A, B, and C. Both tables A and B have a column called id, as well as one other column, Aattribute and Battribute, respectively. Column c also has an id column, and two other columns which hold values for A.id and B.id. Now, in my code, I have easy access to values for both Aattribute and Battribute, and want to delete the row at C, so effectively I want to do something like this:

DELETE FROM C WHERE aid=(SELECT id FROM A WHERE Aattribute='myvalue') AND bid=(SELECT id FROM B WHERE Battribute='myothervalue')

But this obviously doesn't work. Is there any way to make a single complex query, or do I have to run three queries, where I first get the value of A.id using a SELECT with 'myvalue', then the same for B.id, then use those in the final query?
[Edit: it's not letting me comment, so in response to the first comment on this: I tried the above query and it did not work, I figured it just wasn't syntactically correct. Using MS Access, for what it's worth. ]

A: 

You must use IN instead of =.

DELETE
FROM   C
WHERE  aid IN
       (SELECT id
       FROM    A
       WHERE   Aattribute='myvalue'
       )
AND    bid IN
       (SELECT id
       FROM    B
       WHERE   Battribute='myothervalue'
       )
Petar Petrov
Just tried that, did not work. The specific error I'm getting is: "No value given for one or more required parameters." As stated above, I am using MS Access, I don't know if this affects it. I am googling around now for how to use the IN with DELETE in access sql. Thanks for the help.
Joe
That means it can't work out one or other of the field names. It may be a syntax problem - its been a while since I played with access - but fundamentally its not able to work out the fieldnames
Murph
Okay, turned out I had a bad column name in my second SELECT statement. I am no longer getting an error, though my row is not being deleted. I'll keep working on this, thanks.
Joe
Got it all working, just some more minor things on my end. Appreciate all the help!
Joe