oracle 查询语句:
例如:
例如: id name age
1 小明 18
2 王刚 20
3 李丽 16
4 张娜 21
5 金泰 13
条件就是age>=18
显示的结果为:
1 小明 18
2 王刚 20
3 李丽
4 张娜 21
5 金泰
求各位大侠帮忙
------解决方案--------------------
select id, name, case when age>=18 then age else null end
from student;
------解决方案--------------------
------解决方案--------------------
select id,name,age from student where age>18
nuion select id,name,null age from student where age<=18;
------解决方案--------------------
select id,name,age from student where age>18
union select id,name,null age from student where age<=18;
------解决方案--------------------
select id, name, case when age>=18 and age is not null then to_char(age) else '' end agse
from student;
------解决方案--------------------
------解决方案--------------------
- SQL code
with cte AS(SELECT 1 id,'小明' name,18 age FROM DUALUNION ALLSELECT 2, '王刚', 20 FROM DUALUNION ALLSELECT 3, '李丽' ,16 FROM DUALUNION ALLSELECT 4, '张娜' ,21 FROM DUALUNION ALLSELECT 5, '金泰', 13 FROM DUAL)SELECT ID,NAME,DECODE(SIGN(AGE-18),-1,NULL,AGE) AGE FROM CTE;结果如下:ID NAME AGE ---------------------- ---- ---------------------------------------- 1 小明 18 2 王刚 20 3 李丽 4 张娜 21 5 金泰 5 rows selected
------解决方案--------------------
select id, name, case when age>=18 then to_char(age) else null end age from student;
------解决方案--------------------
CREATE table TEST
(
id number,
name varchar2(20),
age number
)
insert into test values (5,'金泰','13');
select * from TEST
1 1 小明 18
2 2 王刚 20
3 3 李丽 16
4 4 张娜 21
5 5 金泰 13
select id,
name,
case
when age > 18 then
age
else
null
end case
from test
1 1 小明
2 2 王刚 20
3 3 李丽
4 4 张娜 21
5 5 金泰
------解决方案--------------------
------解决方案--------------------
SELECT id,name,DECODE(SIGN(AGE-18),-1,'',age) from student