1

I have a database with properties and I need to delete all the properties except for those "owned" by either one of 3 "Inscription Agents". Each property can have up to 2 "owners": agent_inscripteur_1 and agent_inscripteur_2 on the database. This is my code:

DELETE FROM inscriptions 
WHERE (agent_inscripteur_1 != 100520 OR agent_inscripteur_2 != 100520) 
      AND (agent_inscripteur_1 != 97927 OR agent_inscripteur_2 != 97927) 
      AND (agent_inscripteur_1 != 99237 OR agent_inscripteur_2 != 99237)

I think what is happening in my case is that the first part of the code before the AND is executed before the rest, so all properties except for those listed by the first agent are being deleted (by the time it gets to the second and third agent, the properties are all gone).

Can someone please point me in the right direction?

Thanks!

2
  • Actually, in SQL the db has no concept of "first" for Boolean conditions (CASE is an exception for a couple of reasons...). Since for each row at least one of the sub-conditions will (likely) be true, the row is deleted. DBs do this for a couple of reasons: 1) it's actually due to dbs thinking in "sets", essentially matching records 2) it allows the db to choose which condition to evaluate first, allowing queries to be faster. Commented Jun 13, 2014 at 7:06
  • Thanks for making this clear Commented Jun 13, 2014 at 13:24

4 Answers 4

3

What if you simply change your condition like below using NOT IN instead

DELETE FROM inscriptions 
WHERE agent_inscripteur_1 NOT IN (100520,97927,99237)
OR agent_inscripteur_2 NOT IN (100520,97927,99237)
Sign up to request clarification or add additional context in comments.

3 Comments

Thanks, but this deletes 0 rows.
@PalmerDelCampo, change the AND to OR. Most probably the condition is not matching. See edit.
That works, thanks. Please edit your answer to "or" and I'll mark it as correct.
1

Try out this,

DELETE FROM inscriptions 
WHERE ( 
(agent_inscripteur_1 != 100520 AND agent_inscripteur_2 != 100520) 
OR (agent_inscripteur_1 != 97927 AND agent_inscripteur_2 != 97927)
OR (agent_inscripteur_1 != 99237 AND agent_inscripteur_2 != 99237)
) 

1 Comment

No, this will still delete records he wants to keep.
1

I believe the simplest approach when attempting to use negative logic is to write the inverse and then prefix with not. Identify the records of concern and then do not delete them, something like this

DELETE FROM inscriptions 
WHERE not ( (agent_inscripteur_1 = 100520 OR agent_inscripteur_2 = 100520) 
      or (agent_inscripteur_1 = 97927 OR agent_inscripteur_2 = 97927) 
      or (agent_inscripteur_1 = 99237 OR agent_inscripteur_2 = 99237))

You could simplify the query to something like this

DELETE FROM inscriptions 
WHERE not (   agent_inscripteur_1 in (100520, 97927, 99237) 
           OR agent_inscripteur_2 in (100520, 97927, 99237) 
           )

Comments

1

I think you can use here not in operator

DELETE FROM inscriptions WHERE agent_inscripteur_1 not in(100520,97927,99237) 
OR 
agent_inscripteur_2 not in(100520,97927,99237)

1 Comment

Thanks but this deletes 0 rows as well. Bump.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.