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‘);
INSERT INTO lee(name,birthday) VALUES(‘lee‘,‘1980-01-01‘);
INSERT into lee(name,birthday) VALUES(‘jone‘,‘1985-06-01‘);

SELECT * from lee;

3.1例子1
SELECT name,
CASE
WHEN birthday>‘1981‘ THEN ‘old‘
WHEN birthday<‘1988‘ THEN ‘yong‘
ELSE ‘ok‘
END AS age
FROM lee;

3.2例子2
SELECT NAME,
CASE name
WHEN ‘sam‘ THEN ‘yong‘
WHEN ‘lee‘ THEN ‘handsome‘
ELSE ‘good‘
END as oldname
FROM lee;

3.2例子2

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

时间: 2024-10-21 14:54:37

mysql case的相关文章

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

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