Oracle SQL NVL返回多个值

chy5wohz  于 2023-11-17  发布在  Oracle
关注(0)|答案(1)|浏览(189)

当pIsActive为空时,我想列出活动和非活动员工。
pIsActive =(0,1,null)

select e.*
from employee e
where e.id in (1,2,3,4,5)
and (e.isactiv = NVL(pIsActive, (0,1)));

字符串

nnsrf1az

nnsrf1az1#

NVLCOALESCE只返回标量值,而不是值列表。
使用ORIN

SELECT e.*
FROM   employee e
WHERE  e.id IN (1,2,3,4,5)
AND    (  e.isactiv = pIsActive
       OR (pIsActive IS NULL AND e.isactiv IN (0,1))
       );

字符串
您也可以将查询编写为:

SELECT e.*
FROM   employee e
WHERE  e.id IN (1,2,3,4,5)
AND    e.isactiv IN ( NVL(pIsActive, 0), NVL(pIsActive, 1) );


但是我发现第一个选项更容易理解,第二个选项我必须考虑一段时间才能弄清楚,如果pIsActive IS NOT NULLe.isactive IN (0,1)都有效地执行e.isactive = pIsActive

相关问题