case...when 语句的使用

select username ,case username when 'aaa' then '计算机部门'
when 'bbb' then '市场部门' esle '其他部门' end as 部门
from users;

select username ,case when 'aaa' then '计算机部门'
when username = 'bbb' then '市场部' else '其他部门' end as 部门
from users;
select username,case when salary <800 then '工资低'
when salary > '5000' then '工资高' end as 工资水平
from users;
case....when语句的使用
1.格式一-----场景比较单一
case column_name when value1 then result1,....
[else result] end
2.格式二-----场景可以略微复杂
case
when column_name=value1
then result1,....[ELSE result] END
-------------------------------------------------------------
SQL> select username,case username when 'aaa' then '计算机部门'
2 when 'bbb' then '市场部门' else '其他部门' end as 部门
3 from users;
用户名 部门
-------------------- ----------
aaa 计算机部门
bbb 市场部门
ccc 其他部门
cccccc 其他部门
aaa 计算机部门
SQL> select username,case when username='aaa' then '计算机部门'
2 when username='bbb' then '市场部门' else '其他部门' end as 部门
3 from
4 users;
用户名 部门
-------------------- ----------
aaa 计算机部门
bbb 市场部门
ccc 其他部门
cccccc 其他部门
aaa 计算机部门
SQL>
SQL>
SQL> select username,case when salary < 800 then '工资低'
2 when salary>1000 then '工资高' end as 工资水平
3 from users;
用户名 工资水
-------------------- ------
aaa 工资低
bbb 工资低
ccc
cccccc 工资低
aaa
case...when语句

CASE WHEN THEN END




case ... when 语句的使用,可以根据字段不同的值,显示不同结果
CASE 字段名 WHEN 字段当前的值 THEN 满足得出要显示的值 ELSE 都不满足默认显示值 END ; --语法
--示例 username 的值是 aaa 就是计算机部门 ,bbb就是市场部门,不满足默认,其他部门
SELECT ID,
CASE username WHEN 'aaa' THEN '计算机部门'
WHEN 'bbb' THEN '市场部门' ELSE '其他部门'
END AS 部门
FROM xcxuser;
-- 第二种使用方式
CASE WHEN 字段=值 THEN 满足得出要显示的值 ELSE 都不满足默认显示值 END ; --语法 ,这个可以放表达式
--示例
SELECT ID,
CASE WHEN username= 'aaa' THEN '计算机部门'
WHEN username='bbb' THEN '市场部门' ELSE '其他部门'
END AS 部门
FROM xcxuser;
--示例 查询 ,字段满足某个值之间的,满足显示对应的结果
SELECT ID ,
CASE WHEN salary <=800 THEN '工资低'
WHEN salary >=1500 THEN '工资高'
ELSE '工资一般' END AS 工资水平
FROM xcxuser;
select username,case username when 'aaa' then '计算机部门' when 'bbb' then '市场部门' else '其他部门' end as 部门 from users;
select username, case when salary < 800 then '低工资' when salary > 5000 then '高工资' end as 工资水平 from users;
case...when 语句的使用
1、 CASE column_name WHEN value1 THEN result1,... [ELSE result] END
--------------------------------------------------------------------
select username,case username when 'aaa' then '计算机部门'
when 'bbb' then '市场部门' else '其他部门' end as 部门
from users;
-------------------------------------------------------
2、CASE
WHEN column_name=value1 THEN result1,... [else result] end
------------------------------------------------------------------
select username,case when username='aaa' then '计算机部门'
when username='bbb' then '市场部门’else '其他部门' end as 部门
from users;
------------------------------------------------
员工工资小于300工资低,大于600工资高
select username,case when salary<300 then '工资低'
when salary>600 then '工资高' end as 工资情况
from users;
第二种形式可以用来进行逻辑比较?这优点?
case when 的另一种写法 较为灵活
Oracle中case...where语句

作用:根据查询语句中字段不同的值,显示不同的结果。
第一种形式语法:如图,一个case...when语句中可以有多个值和结果,也就是可以有多个when value1 then result1语句,多个语句逗号隔开,else result代表当前面所有条件不满足时给定的结果,该语句通常放置在select语句里面,它通常在查询语句中占据返回字段的位置。
举例:当用户名为aaa时,显示计算机部门,当用户名为bbb时,显示市场部,其他显示其他部门。

第二种case...when形式:它也成为case...when的搜索形式,case后不再有字段名,when后面是表达式。
举例:该种形式比较灵活,以搜索形式。


Oracle中case...where语句
作用:根据查询语句中字段不同的值,显示不同的结果。
第一种形式语法:如图,一个case...when语句中可以有多个值和结果,也就是可以有多个when value1 then result1语句,多个语句逗号隔开,else result代表当前面所有条件不满足时给定的结果,该语句通常放置在select语句里面,它通常在查询语句中占据返回字段的位置。
举例:当用户名为aaa时,显示计算机部门,当用户名为bbb时,显示市场部,其他显示其他部门。

第二种case...when形式:它也成为case...when的搜索形式,case后不再有字段名,when后面是表达式。
举例:该种形式比较灵活,以搜索形式。


select username,case username when 'aaa' then '计算机部门' when 'bbb' then '市场部门' else '其他部门' end as 部门 from users;
select username, case when salary < 800 then '低工资' when salary > 5000 then '高工资' end as 工资水平 from users;
select username,case when salary<800 then'工资低'
when salary>5000 then'工资高' end as 工资水平
from users;
case...when语句
CASE column_name
WHEN value1 THEN RESULT1,...
[ELSE result] END
CASE WHEN column_name=values1 THEN result1,
...【when...then...】
[ELSE result] END
case...when语句的使用
第一种形式:
case col_name
when val1 then result1,...
[else result] end
第二种形式:
case
when col_name=value1
then res1,...[else result] end

case when
使用AS设置别名
case...when...then语句
select username,case when username='aaa' then '计算机部门' when username='bbb' then '市场部门' else '其他部门' and as 部门 from users;
当username为aaa时输出为计算机部门,当username为bbb时输出为市场部门,其他的为其他部门 .
decode函数
select username, decode (username,'aaa','计算机部门''bbb''市场部门','其他')as 部门 from users;
decode函数作用同case...when语句
CASE ... WHEN 语句:
可以根据字段中不同的值来显示不同的结果。
1.CASE column_name
WHEN value1 THEN result1,...
[ELSE result] (当前面的结果都不满足条件的时候,若想给出一个值,就添加上ELSE result)END
2.CASE
WHEN column_name=value1
THEN result1,...[ELSE result] END
SQL> select username,case username when 'aaa' then '计算机部门' 2 when 'bbb' then '市场部门' else '其他部门' end as 部门 3 from users; USERNAME 部门 -------------------- ---------- aaa 计算机部门 bbb 市场部门 ccccc 其他部门 aaa 计算机部门 SQL> select username, case when username='aaa' then '计算机部门' 2 when username='bbb' then '市场部门' else '其他部门' end as 部门 3 from users; USERNAME 部门 -------------------- ---------- aaa 计算机部门 bbb 市场部门 ccccc 其他部门 aaa 计算机部门