Reputation: 5736
Say I have a table
SELECT * INTO MyTable FROM (
VALUES ('col1_a', 'col2_a', 'col3_a', 'coln_a'),
('col1_b', 'col2_b', 'col3_b', 'coln_b'),
('col1_c', 'col2_c', 'col3_c', 'coln_c')
) t (col1, col2, col3, coln)
I only want to delete rows that matches with every single column values I specific (consider rows are highly similar only one or two columns are different)
Of course I can write
DELETE MyTable WHERE col1='col1_a' AND col2='col2_a' AND col3='col3_a' AND coln='coln_a'
DELETE MyTable WHERE col1='col1_b' AND col2='col2_b' AND col3='col3_b' AND coln='coln_b'
But thats too annoying. I want to know if it is possible to write it conveniently by simply listing out the values? I tried this
DELETE MyTable FROM (
VALUES ('col1_a', 'col2_a', 'col3_a', 'coln_a'),
('col1_b', 'col2_b', 'col3_b', 'coln_b')
) t (col1, col2, col3, coln)
but that just removed all the rows in MyTable, please help.
Upvotes: 4
Views: 55
Reputation: 31879
You can use INNER JOIN
on your DELETE
statement:
DELETE t1
FROM MyTable t1
INNER JOIN (
SELECT *
FROM (VALUES
('col1_a', 'col2_a', 'col3_a', 'coln_a'),
('col1_b', 'col2_b', 'col3_b', 'coln_b')
)AS a(col1, col2, col3, coln)
) t2
ON t2.col1 = t1.col1
AND t2.col2 = t1.col2
AND t2.col3 = t1.col3
As commented by Frisbee, you can remove the SELECT *
:
DELETE t1
FROM MyTable t1
INNER JOIN (
VALUES
('col1_a', 'col2_a', 'col3_a', 'coln_a'),
('col1_b', 'col2_b', 'col3_b', 'coln_b')
) AS t2(col1, col2, col3, coln)
ON t2.col1 = t1.col1
AND t2.col2 = t1.col2
AND t2.col3 = t1.col3
Upvotes: 2