详解Mysql case then使用

作者:简单--生活 时间:2024-01-25 05:38:19 

表的创建


CREATE TABLE `lee` (
`id` int(10) NOT NULL AUTO_INCREMENT,
`name` char(20) DEFAULT NULL,
`birthday` datetime DEFAULT NULL,
PRIMARY KEY (`id`)) ENGINE=InnoDB DEFAULT CHARSET=utf8

数据插入:


insert into lee(name,birthday) values ('sam','1990-01-01');
insert into lee(name,birthday) values ('lee','1980-01-01');
insert into lee(name,birthday) values ('john','1985-01-01');

第一种用法:


SELECT name,
CASE WHEN birthday < '1981' THEN 'old'
WHEN birthday > '1988' THEN 'yong'
ELSE 'ok' END YORN
FROM lee

第二种用法:


SELECT NAME, CASE name
WHEN 'sam' THEN 'yong'
WHEN 'lee' THEN 'handsome'
ELSE 'good' END as oldname
FROM lee

第三种:当然了,case when 语句还可以复合


select name, birthday,
case
when birthday > '1983' then 'yong'
when name='lee' then 'handsome'
else 'just so so' end
from lee;

在这里用sql语句进行日期比较的话,需要对年加引号,要不然可能结果和预期的结果不同,
当然也可以用year函数来实现


select name,
case when year(birthday) > 1988 then 'yong'
when year(birthday) < 1980 then 'old'
else 'ok' END
from lee;
==========================================================
create table penalties
(
paymentno INTEGER not NULL,
payment_date DATE not null,
amount DECIMAL(7,2) not null,
primary key(paymentno)
)
insert into penalties values(1,'2008-01-01',3.45);
insert into penalties values(2,'2009-01-01',50.45);
insert into penalties values(3,'2008-07-01',80.45);

第一题:对罚款登记分为三类,第一类low,包括大于0小于等于40的罚款,第二类moderate大于40到80之间的罚款,第三类high包含所有大于80的罚款


select payment_date, amount,
case
when amount >= 0 AND amount < 40 then 'low'
when amount >=40 AND amount < 80 then 'moderate'
when amount >=80 then 'high'
else 'null' END
FROM penalties

第二题:统计出属于low的罚款编号


select * from
( select paymentno, amount,
case
when amount >= 0 AND amount < 40 then 'low'
when amount >=40 AND amount < 80 then 'moderate'
when amount >=80 then 'high'
else 'incorrect' end lvl
from penalties) as p
where p.lvl = 'low'

PS:Mysql,Case When,Case多个字段


select distinct a.PatientID,a.PatientCode,a.PatientSex,a.MobileNo,a.HomePhoneNo,a.UserAge,a.PatientName,a.PatientIDCard, DATE_FORMAT(a.RegistDate,'%Y-%m-%d') as RegistDate,
case when b.usedstartTime is not null and b.UsedEndTime is null then '1'
when b.usedstartTime is not null and b.UsedEndTime is not null then '2'
end as 'usedState'
from mets_v_patient_baseinfo a
left join mets_devices_used_history b on a.patientid = b.PatientID
where  (select ifnull(IsDeleted,0) from userpublic_info where UserID = a.PatientID ) = 0
and 1=1
order by PatientID Desc limit 0,15
标签:mysql,case,then
0
投稿

猜你喜欢

  • 使用Flask-Cache缓存实现给Flask提速的方法详解

    2022-11-12 23:06:03
  • python包相关知识点之包的导入、相对路径以及绝对路径

    2023-08-11 11:09:46
  • python制作一个桌面便签软件

    2021-02-23 05:20:40
  • Numpy对于NaN值的判断方法

    2022-12-15 15:08:21
  • Python实现确认字符串是否包含指定字符串的实例

    2022-04-28 07:40:28
  • Keras自定义IOU方式

    2022-12-24 07:48:27
  • 数据库Sql实现截取时间段和日期实例(SQL时间截取)

    2024-01-27 01:38:29
  • 一篇文章学会两种将python打包成exe的方式

    2022-01-05 23:36:54
  • python 读取DICOM头文件的实例

    2021-12-03 19:30:48
  • pyinstaller打包opencv和numpy程序运行错误解决

    2023-02-23 02:42:54
  • tensorflow 查看梯度方式

    2022-11-07 12:46:22
  • python生成随机验证码(中文验证码)示例

    2022-07-28 05:41:58
  • pyqt 实现QlineEdit 输入密码显示成圆点的方法

    2022-04-16 05:15:55
  • javascript中的后退和刷新实现方法

    2023-08-23 09:36:45
  • Python后台开发Django会话控制的实现

    2022-11-09 22:29:16
  • python基础之Numpy库中array用法总结

    2022-05-10 11:59:53
  • Python实现DBSCAN聚类算法并样例测试

    2022-04-22 22:25:48
  • 将文本输入框内容加入表中的js代码

    2024-04-28 09:50:40
  • Python使用multiprocessing创建进程的方法

    2022-08-03 14:09:21
  • python opencv实现信用卡的数字识别

    2023-07-05 02:20:23
  • asp之家 网络编程 m.aspxhome.com