如何解决SQL选择列表多值,然后使用大小写
我有一个变量,并且基于该变量,我想从选择命令中排除该值。如果我想排除1个字符串,它可以正常工作,但是如果我排除相同条件下的多个字符串,则它不起作用。这是代码。当变量为abc和EFG时,它可以正常工作,但对于XYZ,我想同时排除“ abc”和“ efg”
select table.column1
from table
where column1 not like
case
when variable = 'abc' THEN '%abc%'
when variable = 'EFG' THEN '%efg%'
when variable = 'XYZ' THEN '%abc%' and '%efg%'
else
'_'
END
我尝试使用'%abc%'和'%efg%'和'%(abc | efg)%'和'%abc%'||和|| '%efg%',但似乎没有一个起作用。
解决方法
我想你想要
where (variable = 'abc' and column1 not like '%abc%') or
(variable = 'EFG' and column1 not like '%EFG%') or
(variable = 'XYZ' and column1 not like '%abc%' and column1 not like '%XYZ%') or
(variable not in ('abc','EFG','XYZ'))
,
我认为解决问题的方法不是 when when 情况,而是 sample where 。我在livesql中为此创建了脚本,还分享了以下信息:
create table tab(
id_1 number,column_1 varchar2(20),variable_1 varchar2(20));
insert into tab(id_1,column_1,variable_1) values(8,'13 efg iu','abc');
insert into tab(id_1,variable_1) values(7,'1 3','KDG');
insert into tab(id_1,variable_1) values(6,'_',variable_1) values(5,'XYZ');
insert into tab(id_1,variable_1) values(4,'abc 3 8',variable_1) values(3,'efg SDK','EFG');
insert into tab(id_1,variable_1) values(2,'EFGSSDK',variable_1) values(1,'12abc12','abc');
commit;
select tab.id_1,tab.column_1
from tab
where (variable_1 = 'abc' and column_1 not like '%abc%') or
(variable_1 = 'EFG' and column_1 not like '%efg%') or
(variable_1 = 'XYZ' and column_1 not like '%abc%' and column_1 not like '%efg%') or
(variable_1 not in ('abc','XYZ') and column_1 not like '_');
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。