SQL Select 用 case then 列出多个值
SQL Select List mulitple values with case then
我有一个变量,基于该变量,我想从 select 命令中排除该值。如果我想排除 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%”,但其中 none 个似乎在工作。
我想你想要:
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'))
我认为您的问题的解决方案不是 case 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,column_1,variable_1) values(7,'1 3','KDG');
insert into tab(id_1,column_1,variable_1) values(6,'_','KDG');
insert into tab(id_1,column_1,variable_1) values(5,'_','XYZ');
insert into tab(id_1,column_1,variable_1) values(4,'abc 3 8','XYZ');
insert into tab(id_1,column_1,variable_1) values(3,'efg SDK','EFG');
insert into tab(id_1,column_1,variable_1) values(2,'EFGSSDK','EFG');
insert into tab(id_1,column_1,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', 'EFG', 'XYZ') and column_1 not like '_');
我有一个变量,基于该变量,我想从 select 命令中排除该值。如果我想排除 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%”,但其中 none 个似乎在工作。
我想你想要:
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'))
我认为您的问题的解决方案不是 case 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,column_1,variable_1) values(7,'1 3','KDG');
insert into tab(id_1,column_1,variable_1) values(6,'_','KDG');
insert into tab(id_1,column_1,variable_1) values(5,'_','XYZ');
insert into tab(id_1,column_1,variable_1) values(4,'abc 3 8','XYZ');
insert into tab(id_1,column_1,variable_1) values(3,'efg SDK','EFG');
insert into tab(id_1,column_1,variable_1) values(2,'EFGSSDK','EFG');
insert into tab(id_1,column_1,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', 'EFG', 'XYZ') and column_1 not like '_');