运算符 % 不适用于空值
operator % is not working for null values
假设客户table如下。
cust_id | name | address | language
A | AAA | ZZZZ | en
B | BBB | YYYY |
C | CCC | XXXX | pl
D | DDD | WWWW | en
我想获得一个 plsql 代码,该代码将在用户输入时接受用户输入的语言;
1. 'en'; cust_id: A,B should be the result
2. 'pl'; cust_id: C should be the result
3. doesnt input a value; cust_id: A,B,C,D should be the result
select cust_id from customer c where c.language like NVL('&lang','%');
选项 1 和 2 适用于上述查询,但 3 没有产生任何值。我认为这是因为 % 运算符不使用空值。谁能帮我修改能够为所有 3 个选项提供结果的查询?
您可以简单地使用解码器,如下例所示:
select *
from (select 'en' col1 from dual union all
select 'en' from dual union all
select 'pl' from dual union all
select null from dual) dummy
where decode('&lang', col1, 1, -- If lang is not null and equals col1, return 1, this will only select raws where lang = col1
null, 1, -- if lang is null return 1, this will select every raw
2) = 1 -- Default equals 2 for everything not fitting col1, will only be used if lang is not null
O/P lang = en:
en
en
O/P lang = en:
pl
O/P 语言为空:
en
en
pl
"null"
你的 select 从上面的例子来看应该是这样的:
select cust_id
from customer c
where decode('&lang', col1, 1,
null, 1,
2) = 1;
你可以用 or
:
with customer (cust_id, name, address, language) as
( select 'A', 'AAA', 'ZZZZ', 'en' from dual union all
select 'B', 'BBB', 'YYYY', null from dual union all
select 'C', 'CCC', 'XXXX', 'pl' from dual union all
select 'D', 'DDD', 'WWWW', 'en' from dual )
select * from customer c
where (c.language = '&lang' or '&lang' is null);
您可以使用以下查询:
select cust_id from customer c where c.language = NVL('&lang', c.language);
当 &lang
为 null
时,查询将 return 来自 customer
table 的所有行。
假设客户table如下。
cust_id | name | address | language
A | AAA | ZZZZ | en
B | BBB | YYYY |
C | CCC | XXXX | pl
D | DDD | WWWW | en
我想获得一个 plsql 代码,该代码将在用户输入时接受用户输入的语言;
1. 'en'; cust_id: A,B should be the result
2. 'pl'; cust_id: C should be the result
3. doesnt input a value; cust_id: A,B,C,D should be the result
select cust_id from customer c where c.language like NVL('&lang','%');
选项 1 和 2 适用于上述查询,但 3 没有产生任何值。我认为这是因为 % 运算符不使用空值。谁能帮我修改能够为所有 3 个选项提供结果的查询?
您可以简单地使用解码器,如下例所示:
select *
from (select 'en' col1 from dual union all
select 'en' from dual union all
select 'pl' from dual union all
select null from dual) dummy
where decode('&lang', col1, 1, -- If lang is not null and equals col1, return 1, this will only select raws where lang = col1
null, 1, -- if lang is null return 1, this will select every raw
2) = 1 -- Default equals 2 for everything not fitting col1, will only be used if lang is not null
O/P lang = en:
en
en
O/P lang = en:
pl
O/P 语言为空:
en
en
pl
"null"
你的 select 从上面的例子来看应该是这样的:
select cust_id
from customer c
where decode('&lang', col1, 1,
null, 1,
2) = 1;
你可以用 or
:
with customer (cust_id, name, address, language) as
( select 'A', 'AAA', 'ZZZZ', 'en' from dual union all
select 'B', 'BBB', 'YYYY', null from dual union all
select 'C', 'CCC', 'XXXX', 'pl' from dual union all
select 'D', 'DDD', 'WWWW', 'en' from dual )
select * from customer c
where (c.language = '&lang' or '&lang' is null);
您可以使用以下查询:
select cust_id from customer c where c.language = NVL('&lang', c.language);
当 &lang
为 null
时,查询将 return 来自 customer
table 的所有行。