[当列=…然后删除

问题描述 投票:0回答:1

我正在尝试创建一个查询,如果满足条件,该行将从表中删除。

伪代码:如果indexi = 500并且user = 290045,则从表1中删除,其中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   |
sql sql-server tsql sql-delete
1个回答
2
投票

我认为您想要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 Server中的保留字,因此不是列名的理想选择。

© www.soinside.com 2019 - 2024. All rights reserved.