详解Mysql case then使用

 更新时间:2015年12月25日 15:38:57   作者:简单--生活  
mysql case then在程序开发中经常用到,通过本文给大家介绍mysql case then使用相关知识,对mysql case then相关知识感兴趣的朋友一起学习吧
(福利推荐:【腾讯云】服务器最新限时优惠活动,云服务器1核2G仅99元/年、2核4G仅768元/3年,立即抢购>>>:9i0i.cn/qcloud

(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun

表的创建

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日志文件和日志类型介绍

    Mysql日志文件和日志类型介绍

    这篇文章主要介绍了Mysql日志文件和日志类型介绍,本文讲解了日志文件类型、错误日志、通用查询日志、慢速查询日志、二进制日志等内容,需要的朋友可以参考下
    2014-12-12
  • 浅谈mysql 针对单张表的备份与还原

    浅谈mysql 针对单张表的备份与还原

    下面小编就为大家带来一篇浅谈mysql 针对单张表的备份与还原。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2017-03-03
  • MySql 5.7.14 解压版安装步骤详解

    MySql 5.7.14 解压版安装步骤详解

    本文给大家介绍MySql 5.7.14 解压版安装步骤详解,本文介绍的非常详细,具有参考借鉴价值,感兴趣的朋友一起看下吧
    2016-08-08
  • SQL执行步骤的具体分析

    SQL执行步骤的具体分析

    这篇文章主要介绍了SQL执行步骤的具体分析的相关资料,希望通过本文能帮助到大家,让大家理解掌握SQL是如何执行的,需要的朋友可以参考下
    2017-10-10
  • 通用SQL存储过程分页以及asp.net后台调用的方法

    通用SQL存储过程分页以及asp.net后台调用的方法

    下面小编就为大家带来一篇通用SQL存储过程分页以及asp.net后台调用的方法。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
    2016-12-12
  • MySQL存储表情时报错:java.sql.SQLException: Incorrect string value:‘\xF0\x9F\x92\xA9\x0D\x0A...’的解决方法

    MySQL存储表情时报错:java.sql.SQLException: Incorrect string value:‘

    这篇文章主要给大家介绍了关于MySQL存储表情时报错:java.sql.SQLException: Incorrect string value: '\xF0\x9F\x92\xA9\x0D\x0A...'的解决方法,文中通过示例代码介绍的非常详细,需要的朋友可以参考借鉴,下面来一起看看吧。
    2018-04-04
  • mysql经典4张表问题详细讲解

    mysql经典4张表问题详细讲解

    MySQL是一种关系型数据库管理系统,可以通过连接不同的表将数据进行关联查询,下面这篇文章主要给大家介绍了关于mysql经典4张表问题的相关资料,文中通过代码介绍的非常详细,需要的朋友可以参考下
    2024-03-03
  • MySQL约束和表的复杂查询操作大全

    MySQL约束和表的复杂查询操作大全

    约束是关系型数据库的一个重要功能,?添加到库中的数据需要保证其的正确性;?约束,?就是让数据库帮助程序员更好的检查数据是否正确.,这篇文章主要介绍了MySQL约束和表的复杂查询操作,需要的朋友可以参考下
    2022-11-11
  • Mysql表连接的执行流程详解

    Mysql表连接的执行流程详解

    这篇文章主要介绍了Mysql表连接的执行流程详解,文章围绕主题展开详细的内容介绍,具有一定的参考价值,需要的小伙伴可以参考一下,希望对你的学习有所帮助
    2022-08-08
  • MYSQL不能从远程连接的一个解决方法(s not allowed to connect to this MySQL server)

    MYSQL不能从远程连接的一个解决方法(s not allowed to connect to this MySQL s

    MYSQL不能从远程连接的一个解决方法(s not allowed to connect to this MySQL server)
    2011-08-08

最新评论

?


http://www.vxiaotou.com