mysql 语句case when

mysql
语句case when

select USER_ID ,USER_NAME ,CASE WHEN  atten.DESTINATION_ID is null THEN FALSE ELSE TRUE END  as attentioned    from T_SD_USER as user LEFT OUTER JOIN T_SD_ATTENTION as atten on `user`.USER_ID = atten.ORIGIN_ID and atten.DESTINATION_ID = 3001 where `user`.USER_ID = 3000;

表的创建

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‘);

使用case when语句

1。


select name,
case
when birthday<‘1981‘ then ‘old‘
when birthday>‘1988‘ then ‘yong‘
else ‘ok‘ END YORN
from lee;

2。


select NAME,
case name
when ‘sam‘ then ‘yong‘
when ‘lee‘ then ‘handsome‘
else ‘good‘ end
from lee;

当然了case when语句还可以复合

3。


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

在这里用sql语句进行日期比较的话,需要对年加引号。要不然可能结果可能和预期的结果会不同。我的mysql版本5.1

当然也可以用year函数来实现,以第一个sql为例


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);

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

2.#统计出属于low的罚款编号。

第一道题的解法与上面的相同


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`

2.#统计出属于low的罚款编号。重点看这里的解决方法
方法1.


select paymentno,amount
from `penalties`
where 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 =‘low‘;

方法2


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‘;

来自 http://www.cnblogs.com/john2000/archive/2010/09/21/1832729.html

时间: 2024-08-27 05:04:01

mysql 语句case when的相关文章

MySQL 存储过程CASE语句用法

MySQL提供了一个替代的条件语句CASE. MySQL CASE语句使代码更加可读和高效. CASE语句有两种形式:简单的搜索CASE语句.下面讲讲MySQL 存储过程CASE语句用法. 1,CASE语法结构 CASE case_expression WHEN when_expression_1 THEN commands WHEN when_expression_2 THEN commands ... ELSE commands END CASE; 2,CASE应用实例 DELIMITER

在mysql语句中为什么要加反引号

在MySQL语句中我们有时候经常会遇到反引号(``),刚开始的时候不知道什么意思,他是什么作用呢? Select * from `member` order by posts desc limit 0,10; 它是为了区分MYSQL的保留字与普通字符而引入的符号. 举个例子:SELECT `select` FROM `test` WHERE select='字段值' 在test表中,有个select字段,如果不用反引号,MYSQL将把select视为保留字而导致出错,所以,有MYSQL保留字作为

常用的MySQL语句写法

常用的MySQL语句写法 MySQL的SQL语句写法,除了那些基本的之外,还有一些也算比较常用的,这里记录下来,以便以后查找.     好记性不如烂笔头,这话说的太有道理了,一段时间不写它,还真容易忘记.大家不要纠结这些SQL语句包含的业务或是其它问题,本文只是一篇笔记而已.     将数据从T1表导入到T2表 INSERT INTO T2 (C1,C2) SELECT C1,C2 FROM T1 [WHERE C1 = XX AND C2 = XX ORDER BY C1] 使用T2表的NAM

mysql语句:批量更新多条记录的不同值[转]

mysql语句:批量更新多条记录的不同值[转] mysql语句:批量更新多条记录的不同值 mysql更新语句很简单,更新一条数据的某个字段,一般这样写: 1 UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_value'; 如果更新同一字段为同一个值,mysql也很简单,修改下where即可: 1 UPDATE mytable SET myfield = 'value' WHERE other_field in ('o

常用的MySql语句

/* 启动MySQL */net start mysql /* 连接与断开服务器 */mysql -h 地址 -P 端口 -u 用户名 -p 密码 /* 跳过权限验证登录MySQL */mysqld --skip-grant-tables-- 修改root密码密码加密函数password()update mysql.user set password=password('root'); SHOW PROCESSLIST -- 显示哪些线程正在运行SHOW VARIABLES -- /* 数据库操

mysql语句:批量更新多条记录的不同

mysql更新语句很简单,更新一条数据的某个字段,一般这样写: UPDATE mytable SET myfield = 'value' WHERE other_field = 'other_value'; 如果更新同一字段为同一个值,mysql也很简单,修改下where即可: UPDATE mytable SET myfield = 'value' WHERE other_field in ('other_values'); 这里注意 ‘other_values’ 是一个逗号(,)分隔的字符串

Mysql when case 批量更新

UPDATE categories SET display_order = CASE id WHEN 1 THEN 3 WHEN 2 THEN 4 WHEN 3 THEN 5 END WHERE id IN (1,2,3); 这句sql的意思是,更新display_order 字段,如果id=1 则display_order 的值为3,如果id=2 则 display_order 的值为4,如果id=3 则 display_order 的值为5. 即是将条件语句写在了一起. 这里的where部分

总结今天学习的mysql语句

关键字 进入mysql:mysql -uroot -p 查看数据库:show databases 进入数据库:use DATABASE'S_NAME 查看数据库中表:show tables 以上在doc下演示,以后的在工具中演示 mysql中大小写不敏感 创建库:create database if not exists DATABASE'S_NAME 创建表:create table  if not exists  TABLE'S_NAME(列名1 属性,列名2 属性,......) 1 cr

【夯实Mysql基础】记一次mysql语句的优化过程!

1. [事件起因] 今天在做项目的时候,发现提供给客户端的接口时间很慢,达到了2秒多,我第一时间,抓了接口,看了运行的sql,发现就是 2个sql慢,分别占了1秒多. 一个sql是 链接了5个表同时使用了 2个 order by和 1个limit的分页 sql. 一个sql是上一个sql的count(*),即链接了5个表,当然没有limit了(取总数). 2. [着手优化] 1)[优化思路] 第一条是 做client调用 service层的数据缓存 第二条就是 优化sql本身. 这里着重讲一下