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'
mysql case then使用的更多相关文章
- 阳性比例 mysql CASE UNION ALL
阳性比例 mysql CASE UNION ALL SELECT t.*,t.type_0/all_ FROM ( SELECT FROM_UNIXTIME(create_time,'%Y-%m-%d ...
- mysql case when
下面为您举例说明了三种mysql中case when语句的使用方法,供您参考学习,如果您对mysql中case when语句使用方面感兴趣的话,不妨一看. 1. select name, case w ...
- mysql case when 条件过滤
[1].[代码] 使用CASE WHEN进行字符串替换处理 跳至 [1] [2] [3] [4] ? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 2 ...
- mysql case when group by实例
mysql 中类似php switch case 的语句. select xx字段, case 字段 when 条件1 then 值1 when 条件2 then 值2 else 其他值 END 别名 ...
- mysql case的语法
测试表:team 第一种语法: CASE case_value WHEN when_value THEN statement_list [WHEN when_value THEN statement_ ...
- mysql case....when条件
oracle的写法SELECT decode(ttype,1,’a',2,’b',3,’c',’d') FROM taba 可以在mysql里写成SELECT if(ttype=1, 'a',if(t ...
- MySQL case when then 用法
下面演示一下MYSQL中的CASE WHEN THEN的用法. 一. SELECT MENU_NAME, YXBZ, case YXBZ when 'Y' then '开放' when 'N' the ...
- mysql case when then end学习
表 vtiger_acctive,字段 id,name. 1. 查询中使用 # 查询如果name的值为 hello1 时输出 6666,当值为 hello2 时,输出 333333 select ca ...
- mysql case when用法
SELECT CASE WHEN `categoryid` =1THEN '参赛队员'ELSE '指导老师'END FROM `blog_article` WHERE 1
随机推荐
- ZOJ 2136 Longest Ordered Subsequence
#include<time.h> #include <cstdio> #include <iostream> #include<algorithm> # ...
- Arch Linux Installation Guide
Arch Linux Installation Guide timedatectl set-ntp true sed -i '/Score/{/China/!{n;s/^/#/}}' /etc ...
- 使用PHP获取时间今天 明天 昨天 时间戳的详解
使用php获取时间今天明天昨天时间戳 2013-06-20 11:12 <?php echo "今天:".date("Y-m-d")."< ...
- 腾讯微博的账号登录及api操作
.tqq.php <?php /** * PHP Library for t.qq.com * * @author */ class tqqPHP { function __construct( ...
- IIS7报错
错误内容:”未能加载文件或程序集“IWMS_Admin”或它的某一个依赖项.试图加载格式不正确的程“ 解决方法:进入IIS“应用程序池”,然后在右边列表中,选中当前网站所使用的程序池,打开右侧的“高级 ...
- 第十八篇:在SOUI中实现PreTranslateMessage
在MFC中,通常可以通过重载CWnd::PreTranslateMessage这样一个虚函数来实现对一些窗口消息的预处理.多用于tooltip的显示控制. 在SOUI中也实现了类似的机制. 要在SOU ...
- Android JNI开发生成.h头文件问题(转)
在JNI开发中,首先要将建立的anroid类编译成.h文件,编译用到命令javah,由于第一次用,以前对java的编译过程也不怎么了解,所以走了好多弯路,网络没有对这一步的详细介绍,这里讲一下: 通过 ...
- 接着上一篇 《Is WPF dead》
最近美国的PM传来消息,说微软在收集开发者的意见,会对WPF进行改进,微软会主要在1) performance 2) interop 3) touch and 4) access to WinRT A ...
- .NET C# Tostring() format 格式化字符串大全
C 货币 2.5.ToString("C") ¥2.50 D 十进制数 25.ToString("D5") 00025 E 科学型 25000.ToString ...
- 如何给你的ASP.NET页面添加HelpPage
如何给你的ASP.NET页面添加HelpPage 最近写了一些webAPI,所以需要搞一套API的帮助文档,google了一下,发现这是可以自动生成的,以下就是如何自动生成HelpPage的说明. 参 ...