如何在LOV中实现基于另一多选LOV的NOT IN查询?
解决LOV2排除LOV1选中值的问题
你遇到的问题核心是:LOV1多选后传递的是逗号分隔的字符串,直接用not in (:student_id)时,SQL会把整个字符串当作单个值处理(比如not in ('1,2,3')),而非多个独立的ID,所以过滤逻辑不生效。
以下是几种可行的解决方法:
方法1:字符串拆分后用NOT IN(Oracle环境)
利用Oracle的REGEXP_SUBSTR函数把逗号分隔的字符串拆分为多行,再进行排除:
select distinct stud_id from employee where stud_id not in ( select trim(regexp_substr(:student_id, '[^,]+', 1, level)) from dual connect by regexp_substr(:student_id, '[^,]+', 1, level) is not null )
方法2:用NOT EXISTS替代(性能更优)
当数据量较大时,NOT EXISTS比NOT IN效率更高,同样结合字符串拆分:
select distinct e.stud_id from employee e where not exists ( select 1 from dual where trim(regexp_substr(:student_id, '[^,]+', 1, level)) = e.stud_id connect by regexp_substr(:student_id, '[^,]+', 1, level) is not null )
方法3:MySQL环境用FIND_IN_SET
如果是MySQL数据库,可以直接用FIND_IN_SET函数判断ID是否在逗号分隔的字符串中:
select distinct stud_id from employee where not find_in_set(stud_id, :student_id)
额外注意:处理LOV1未选中的情况
如果LOV1没有选中任何值,:student_id会是空字符串,此时上述Oracle查询会因not in ()报错,需要添加空值判断:
select distinct stud_id from employee where (:student_id is null or stud_id not in ( select trim(regexp_substr(:student_id, '[^,]+', 1, level)) from dual connect by regexp_substr(:student_id, '[^,]+', 1, level) is not null ))
内容的提问来源于stack exchange,提问作者kiric8494
相关产品推荐
相关产品推荐

