说我有一张桌子
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)
我只想删除与我特定的每个列值匹配的行(考虑行非常相似,只有一两列不同)
当然我可以写
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'
但那太烦人了。我想知道是否可以通过简单地列出值来方便地编写它?我试过这个
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)
但是刚刚删除了MyTable中的所有行,请提供帮助。
答案 0 :(得分:2)
您可以在INNER JOIN
声明中使用DELETE
:
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
如Frisbee所述,您可以删除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