SQL基于两列或更多列删除行

时间:2017-10-31 13:41:43

标签: sql sql-server tsql

我试图从我的表中删除'ID'列为NULL的所有行,除了'group column'是Everyone'的行。我怎样才能做到这一点?我尝试了上述查询的不同组合,但没有一个有效。

delete from LANReporter where NOT [Group] = 'everyone' AND [ID] IS NULL AND 
[Server] = 'sv73938'

4 个答案:

答案 0 :(得分:1)

如果Group可以为NULL,则需要执行此操作:

WHERE (Group IS NULL OR [Group] <> 'everyone') AND [ID] IS NULL AND 
[Server] = 'sv73938'

答案 1 :(得分:0)

使用“不等于”条件!=

  DELETE FROM LANReporter 
  WHERE [Group] != 'everyone'
      AND [ID] IS NULL
      AND [Server] = 'sv73938';

如果这仍然不能解决问题,那么我怀疑你的数据存在问题(也许你的NULL实际上不是NULL,而是空字符串或空格)。

答案 2 :(得分:0)

你可以试试这个

delete from LANReporter where [id] is null and [group] not in ('everyone') and [Server] = 'sv73938';

答案 3 :(得分:0)

您可以使用:

delete LANReporter where [Group] <> 'everyone' AND [ID] IS NULL AND 
[Server] = 'sv73938'