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‘

时间: 2024-11-08 19:19:13

mysql case then使用的相关文章

MySQL case when 用法 详细举例,可创建数据演示

首先我们创建数据库表: CREATE TABLE `t_demo` ( `id` int(32) NOT NULL, `name` varchar(255) DEFAULT NULL, `age` int(2) DEFAULT NULL, `num` int(3) DEFAULT NULL, PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 插入数据: INSERT INTO `t_demo` VALUES ('1', '张三',

mysql case when用户示例

看到一些朋友在使用bind-dlz时写的mysql语句一知半解,索性自己动手写几个示例实验一下,加深印象,废话不多说,直解上料: 1)原始记录 mysql> select id,name from test;+------+------+| id   | name |+------+------+|    1 | lili ||   10 | yaya ||   30 | cici |+------+------+ 2)语法CASE input_expression    WHEN when_e

MySQL CASE表达式

一.MySQL为您提供了两种形式的CASE表达式. 1.CASE表达式的第一种形式: CASE value WHEN compare_value_1 THEN result_1 WHEN compare_value_2 THEN result_2 WHEN compare_value_3 THEN result_3 WHEN compare_value_4 THEN result_4 WHEN compare_value_5 THEN result_5 WHEN compare_value_6

mysql case

1.table CREATE TABLE `lee`(`id` INT(10) NOT NULL auto_increment,`name` varchar(20) DEFAULT null,`birthday` datetime DEFAULT null,PRIMARY KEY(`id`)) ENGINE=INNODB DEFAULT CHARSET=utf8; 2.data INSERT INTO lee(name,birthday) VALUES('sam','1990-01-01');I

mysql case when

select a.*, case when a.age = 1 then '一岁了' when a.age = 2 then '二岁了' when a.age >= 3 and a.age <= 8 then ( case when a.age = 3 then '三岁了' when a.age = 4 then '四岁了' when a.age = 5 then '五岁了' when a.age = 6 then '六岁了' when a.age = 7 then '七岁了' when a.

MySQL case when条件分支判断

举例说明 SELECT CASE a.AFTER_QUALITY WHEN '0' THEN '过免维期' WHEN '1' THEN '未过免维期' ELSE '错误数据' END AS AFTER_QUALITY FROM t_maintain_log a 数据库表内容: 运行结果:

mysql case when &amp; concat &amp; SUBSTRING_INDEX &amp; not &amp; having 使用的小case

1. 代码 SELECT a.id, a.activity_name, ( CASE WHEN a.activity_end_time > now() THEN '参与中' ELSE ( CASE WHEN ( a.activity_doubt <> '*' AND a.activity_doubt < c.doubt ) OR ( a.activity_praise <> '*' AND a.activity_praise > c.praise * 100 )

Mysql CASE WHEN 用法

select sum(1) as col_0_0_, sum(case vciinfo.useable when -1 then 1 else 0 end) as col_1_0_, sum(case when vciinfo.activatetime is null then 0 else 1 end) as col_2_0_ from vci_info vciinfo 这是一个用case when 实现统计表中不同状态数据数量的sql语句 这里要注意的是 判断字段是否为空用 case whe

MySQL case when 使用

程序里错误码和字典对应不上,要做二次加工,回顾了一下case when的使用,直接在SQL里处理一下 case when的两种写法 简单判断: 要判断值的列写在case后面,when后面写值,相等的情况执行then后的语句,可以写多个when判断语句,都不满足返回else后的值,要以end结尾,end后面可以给该列指定列名. CASE [col_name] WHEN [value1] THEN [result1] WHEN [value1] THEN [result2] .... ELSE [d