Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sql deleting multiple rows of different columns

Tags:

sql

mysql

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?

like image 620
Zach Smith Avatar asked Mar 13 '26 06:03

Zach Smith


2 Answers

This will do it:

DELETE FROM tableName 
WHERE ColumnA = 123 AND ColumnB IN (1, 2, 3, 4, 8)
like image 103
forpas Avatar answered Mar 15 '26 21:03

forpas


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);
like image 31
Yogesh Sharma Avatar answered Mar 15 '26 21:03

Yogesh Sharma



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!