当列 = ... 然后删除
When column = ... then delete
我正在尝试创建一个查询,如果满足条件,该行将从 table 中删除。
伪代码:如果 indexi
= 500 且 user
= 290045,则从 table1 中删除 indexi
= 85 且 user
= 290045
user | line_no | header | indexi |
-------+---------+--------+--------+
290045 | 0 | 0 | 500 |
290045 | 1 | 0 | 85 |
733 | 0 | 0 | 33 |
预期结果:
user | line_no | header | indexi |
-------+---------+--------+--------+
290045 | 0 | 0 | 500 |
733 | 0 | 0 | 33 |
我想你想要 exists
:
delete t
from mytable t
where
t.indexi = 85
and t.usr = 290045
and exists (select 1 from mytable t1 where t1.usr = t.usr and t1.indexi = 500)
旁注:usr
是 SQL 服务器中的保留字,因此不是列名的好选择。
我正在尝试创建一个查询,如果满足条件,该行将从 table 中删除。
伪代码:如果 indexi
= 500 且 user
= 290045,则从 table1 中删除 indexi
= 85 且 user
= 290045
user | line_no | header | indexi |
-------+---------+--------+--------+
290045 | 0 | 0 | 500 |
290045 | 1 | 0 | 85 |
733 | 0 | 0 | 33 |
预期结果:
user | line_no | header | indexi |
-------+---------+--------+--------+
290045 | 0 | 0 | 500 |
733 | 0 | 0 | 33 |
我想你想要 exists
:
delete t
from mytable t
where
t.indexi = 85
and t.usr = 290045
and exists (select 1 from mytable t1 where t1.usr = t.usr and t1.indexi = 500)
旁注:usr
是 SQL 服务器中的保留字,因此不是列名的好选择。