在DB2中结合IN和LIKE函数。

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

在DB2中是否有办法将IN和LIKE函数结合在一起?例如,我想排除用户ID为A,B,C的用户和从X%或Y%开始的用户ID。我尝试了下面的查询,但是没有成功。

select * from table where userid not in ('A','B','C') or (not like 'X%' or not like 'Y%')
db2
1个回答
0
投票

你可以使用所有的常量在 INLIKE:

with 
  table (userid) as 
(
values 'A', 'AA', 'XX', 'YY', 'ZZ'
)
, vals (userid) as 
(
values 'A', 'B', 'C', 'X%', 'Y%'
)
select * 
from table t
where not exists
(
select 1
from vals v
where t.userid like v.userid
);

结果是:

|USERID|
|------|
|AA    |
|ZZ    |

1
投票

用 "AND "代替 "OR"。

select * from table 
where userid not in ('A','B','C') 
and userid not like 'X%' 
and userid not like 'Y%'
© www.soinside.com 2019 - 2024. All rights reserved.