2

I have a table and I need to delete rows by two columns, for example

+------+------+--------+
| col1 | col2 |  other |
+------+------+--------+
|  12  |   2  |  test  |
+------+------+--------+
|  14  |   2  |  test1 |
+------+------+--------+
|  12  |   3  |  test2 |
+------+------+--------+
|  13  |   3  |  test3 |
+------+------+--------+
|  15  |   4  |  test4 |
+------+------+--------+

and I want to delete rows that have (col1,col2) pair equal any values in (12,2),(13,3),(14,2)

Can I do this by pure SQL?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Ariyan
  • 14,760
  • 31
  • 112
  • 175

2 Answers2

4

If you have a lot of values, populate a table with them and do:

DELETE t
FROM Table t
INNER JOIN TempTable tt
ON t.col1 = tt.col1
AND t.col2 = tt.col2
JNK
  • 63,321
  • 15
  • 122
  • 138
  • 1
    Note that this solution does not work with sqlite. See this SO solution for sqlite: https://stackoverflow.com/questions/24511153/how-delete-table-inner-join-with-other-table-in-sqlite – MOK9 Mar 10 '19 at 17:47
1

Try the following sql:

delete from <tablename> 
where (col1 = 12 and col2 = 2) 
   or (col1 = 13 and col2 = 3) 
   or (col1 = 14 and col2 = 2)
Howard
  • 38,639
  • 9
  • 64
  • 83