首页 > 解决方案 > SQL server WHERE 子句中的排除

问题描述

我只想排除如果 ColumnA = 'SA' 排除 ColumnB 不像 '%Prev%' 和 ColumnB 不像 '%old%'。如果 columnA = 'BA' 我想保留它们。经验:

select columnA
    from Table
    where columnA ='SA' and ColumnB not like '%Prev%' and ColumnB not like '%old%'

标签: sqlsql-serverdatabase

解决方案


你想要or

select columnA
from Table
where (columnA = 'SA' and ColumnB not like '%Prev%' and ColumnB not like '%old%') or
      columnA <> 'SA'

如果columnA可以NULL,逻辑也需要考虑到这一点。


推荐阅读