详解Mysql case then使用
作者:简单--生活 发布时间:2024-01-25 05:38:19
标签:mysql,case,then
表的创建
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


猜你喜欢
- 程序二:addforum.php <html> <head> <link&
- 运行效果实现代码# -*- coding: utf-8 -*-import tkinter as tkinterimport tkinter
- 创建 var d=new Date(); 要注意的是在JavaScript中月份的值是从0到11(0表示1月)。 设置日期和时间值 设置日期
- 1、更新NVIDIA驱动 选对应自己显卡的驱动,(选studio版本,不要game版本)驱动链接 2、添加Anacond
- 先从String的扩展开始吧,后面有一部分的扩展要依赖这里扩展的方法。为了更加清晰和详细,我会一个方法一个方法地贴出来,你完全可以把所有的方
- 引言所有的层都具有的参数,如name, type, bottom, top和transform_param请参看我的前一篇文章:Caffe卷
- 目录楔子使用方法创建一个文件创建一个目录重命名将文件1.txt移动到子目录test_child, 所以此时会伴随一个创建、一个删除以及一个修
- 当我们修改一份代码的时候,也许会碰到修改后的代码还不如修改之前的代码能够满足自己的需求,那么这个时候我们就需要对代码进行回滚,下面我们来看一
- 本文实例为大家分享了python tkinter实现弹窗输入输出的具体代码,供大家参考,具体内容如下代码如下:from tkinter im
- 获取文件目录的方法 :import os# '***获取当前目录***'os.getcwd()# '***获取上级目
- 0、干货先写在前1、前端传值的数据必须使用JSON.stringify()传化2、后端,通过request.body接收数据,直接使用jso
- Microsoft SQL Server错误: 15138删除对于用户失败,数据库主体在该数据库中拥有架构,无法删除解决方法一先删除此用户对
- 最近项目中的资产的任务状态频频出现问题,查看日志文件,看代码逻辑,也没发现什么具体的错误,总是过段时间就会出现一个表的字段没有更新的问题,很
- 一:unittest是python自带的一个单元测试框架,类似于java的junit,基本结构是类似的。基本用法如下: 1.用import
- 本文将教会我们如何使用PyQt5控件的工具提示功能。#!/usr/bin/python3# -*- coding: utf-8 -*-&qu
- 本文实例为大家分享了js实现瀑布流效果的具体代码,供大家参考,具体内容如下CSS样式:<style> .cont{margin:
- 定时器1-"*/5 * * * * *"package mainimport ("fmt""
- 在实际的工作和学习中,许多人的SQL Server 2005数据库日志文件可能会发生损坏,例如硬件故障、计算机非正常重启或关机等等。在SQL
- 按照固定的字符,拆分已有的字符串split(sep, n, expand = False):sep:用于分割的字符串n:分割为多少列expa
- 我们主要讲解一下利用Python实现感知机算法。算法一首选,我们利用Python,按照上一节介绍的感知机算法基本思想,实现感知算法的原始形式