在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%')
你可以使用所有的常量在 IN
在 LIKE
:
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 |
用 "AND "代替 "OR"。
select * from table
where userid not in ('A','B','C')
and userid not like 'X%'
and userid not like 'Y%'