对 CASE WHEN SQL 的结果执行多值 LIKE
Performing a multi value LIKE on the result of a CASE WHEN SQL
是否可以对 CASE WHEN.
的结果执行多值 LIKE(LIKE "x" OR "y" ..)
我想达到的目标:
((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "a" OR "b" or "d")
我想避免做的事情:
((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "a")
OR ((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "b")
OR ((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "c")
您可以将主查询与子查询结合起来。检查这个:
select customerName from
(
select 'fff' customerName union all
select null
) customer
where exists (
select 1 from
(
select 'a' x union all
select 'b' union all
select 'd'
) i
where (CASE WHEN customerName IS NULL THEN 'abc' ELSE 'def' END) LIKE i.X || '%'
)
| customername |
| :----------- |
| fff |
| null |
db<>fiddle here
您可以使用 ~
运算符来执行类似正则表达式的表达式。
例如,假设您具有以下值:
'abc'
'qabc'
'ptestp'
'sometext'
'oneone'
如果你只想select包含abc
或test
的那些,你可以执行以下查询:
SELECT * FROM (VALUES ('abc'),
('qabc'),
('ptestp'),
('sometext'),
('oneone'))
example_data(label)
WHERE label ~ 'abc|test';
这只会 select 以下值:
abc
, qabc
, ptestp
.
请记住,~
运算符接受右侧的正则表达式,因此您可以使用任何类型的模式(如完全匹配、在单词开头匹配、在单词结尾匹配等) ).
例如以下查询:
SELECT * FROM (VALUES ('abc'),
('abcc'),
('ptestp'),
('sometext'),
('oneone'))
example_data(label)
WHERE label ~ '^abc$|^test$';
只会select第一行(abc
),因为它要求单词完全匹配。
是否可以对 CASE WHEN.
的结果执行多值 LIKE(LIKE "x" OR "y" ..)我想达到的目标:
((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "a" OR "b" or "d")
我想避免做的事情:
((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "a")
OR ((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "b")
OR ((CASE WHEN customerName IS NULL THEN "abc" ELSE "def" END) LIKE "c")
您可以将主查询与子查询结合起来。检查这个:
select customerName from ( select 'fff' customerName union all select null ) customer where exists ( select 1 from ( select 'a' x union all select 'b' union all select 'd' ) i where (CASE WHEN customerName IS NULL THEN 'abc' ELSE 'def' END) LIKE i.X || '%' )
| customername | | :----------- | | fff | | null |
db<>fiddle here
您可以使用 ~
运算符来执行类似正则表达式的表达式。
例如,假设您具有以下值:
'abc'
'qabc'
'ptestp'
'sometext'
'oneone'
如果你只想select包含abc
或test
的那些,你可以执行以下查询:
SELECT * FROM (VALUES ('abc'),
('qabc'),
('ptestp'),
('sometext'),
('oneone'))
example_data(label)
WHERE label ~ 'abc|test';
这只会 select 以下值:
abc
, qabc
, ptestp
.
请记住,~
运算符接受右侧的正则表达式,因此您可以使用任何类型的模式(如完全匹配、在单词开头匹配、在单词结尾匹配等) ).
例如以下查询:
SELECT * FROM (VALUES ('abc'),
('abcc'),
('ptestp'),
('sometext'),
('oneone'))
example_data(label)
WHERE label ~ '^abc$|^test$';
只会select第一行(abc
),因为它要求单词完全匹配。