2

I am working on a way to delete multiple rows of a single SQL database, targeting two columns. ColumnA will always be the same value, and ColumnB will be different values.

I am trying to figure out the correct syntax for something like this:

DELETE FROM tableName WHERE (
    ColumnA = 123 AND ColumnB = 1,
    ColumnA = 123 AND ColumnB = 2,
    ColumnA = 123 AND ColumnB = 3,
    ColumnA = 123 AND ColumnB = 4,
    ColumnA = 123 AND ColumnB = 8
);

What will be the correct SQL syntax for the above logic?

3
  • 4
    Just replace the commas with ORs. Commented Jan 7, 2020 at 13:03
  • Oh yeah, you even may not need to add parentheses as ANDs are interpreted before ORs already: dev.mysql.com/doc/refman/8.0/en/operator-precedence.html Commented Jan 7, 2020 at 13:10
  • 2
    Try this, DELETE FROM tableName WHERE ( ColumnA = 123 AND ColumnB in ( 1, 2,3,4,8); Commented Jan 7, 2020 at 16:55

3 Answers 3

10

This will do it:

DELETE FROM tableName 
WHERE ColumnA = 123 AND ColumnB IN (1, 2, 3, 4, 8)
Sign up to request clarification or add additional context in comments.

Comments

8

Replace , with OR :

where (ColumnA = 123 and ColumnB = 1) or
      (ColumnA = 123 and ColumnB = 2) or
        . . .
      (ColumnA = 123 and ColumnB = 8);

This can be also simplified as :

where ColumnA = 123 and
      ColumnB in (1, 2, 3, 4, 8);

Comments

7

The simplest solution (as suggested by GMB) is to replace the commas with OR. However, MySQL also supports tuples, so you can use:

WHERE (ColumnA, ColumnB) IN ( (123, 1), (123, 2), (123, 3), (123, 4), (123, 8) )

Or, if the ColumnA values really are all the same, then:

WHERE ColumnA = 123 AND
      ColumnB IN (1, 2, 3, 4, 8)

Comments

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.