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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
/*mysql> select * from sales;+-----+------------+--------+--------+--------+------+------------+| num | name | winter | spring | summer | fall | category |+-----+------------+--------+--------+--------+------+------------+| 1 | Java | 1067 | 200 | 150 | 267 | Holiday || 2 | C | 970 | 770 | 531 | 486 | Profession || 3 | JavaScript | 53 | 13 | 21 | 856 | Literary || 4 | SQL | 782 | 357 | 168 | 250 | Profession || 5 | Oracle | 589 | 795 | 367 | 284 | Holiday || 6 | MySQL | 953 | 582 | 336 | 489 | Literary || 7 | Cplus | 752 | 657 | 259 | 478 | Literary || 8 | Python | 67 | 23 | 83 | 543 | Holiday || 9 | PHP | 673 | 48 | 625 | 52 | Profession |+-----+------------+--------+--------+--------+------+------------+9 rows in set (0.01 sec)mysql> SELECT name AS Name, -> CASE category -> WHEN "Holiday" THEN "Seasonal" -> WHEN "Profession" THEN "Bi_annual" -> WHEN "Literary" THEN "Random" END AS "Pattern" -> FROM sales;+------------+-----------+| Name | Pattern |+------------+-----------+| Java | Seasonal || C | Bi_annual || JavaScript | Random || SQL | Bi_annual || Oracle | Seasonal || MySQL | Random || Cplus | Random || Python | Seasonal || PHP | Bi_annual |+------------+-----------+9 rows in set (0.00 sec)*/Drop table sales; CREATE TABLE sales( num MEDIUMINT NOT NULL AUTO_INCREMENT, name CHAR(20), winter INT, spring INT, summer INT, fall INT, category CHAR(13), primary key(num))type=MyISAM;insert into sales value(1, 'Java', 1067 , 200, 150, 267,'Holiday');insert into sales value(2, 'C',970,770,531,486,'Profession');insert into sales value(3, 'JavaScript',53,13,21,856,'Literary');insert into sales value(4, 'SQL',782,357,168,250,'Profession');insert into sales value(5, 'Oracle',589,795,367,284,'Holiday');insert into sales value(6, 'MySQL',953,582,336,489,'Literary');insert into sales value(7, 'Cplus',752,657,259,478,'Literary');insert into sales value(8, 'Python',67,23,83,543,'Holiday');insert into sales value(9, 'PHP',673,48,625,52,'Profession');select * from sales;SELECT name AS Name,CASE categoryWHEN "Holiday" THEN "Seasonal"WHEN "Profession" THEN "Bi_annual"WHEN "Literary" THEN "Random" END AS "Pattern"FROM sales; |
[2].[代码] 简单语句 跳至 [1] [2] [3] [4]
|
1
2
3
4
|
SELECT CASE WHEN 10*2=30 THEN '30 correct' WHEN 10*2=40 THEN '40 correct' ELSE 'Should be 10*2=20'END; |
[3].[代码] 多重表达式 跳至 [1] [2] [3] [4]
|
1
2
3
4
5
|
SELECT CASE 10*2 WHEN 20 THEN '20 correct' WHEN 30 THEN '30 correct' WHEN 40 THEN '40 correct'END; |
[4].[代码] 在SELECT查询中使用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
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
|
/*mysql> SELECT Name, RatingID AS Rating, -> CASE RatingID -> WHEN 'R' THEN 'Under 17 requires an adult.' -> WHEN 'X' THEN 'No one 17 and under.' -> WHEN 'NR' THEN 'Use discretion when renting.' -> ELSE 'OK to rent to minors.' -> END AS Policy -> FROM DVDs -> ORDER BY Name;+-----------+--------+------------------------------+| Name | Rating | Policy |+-----------+--------+------------------------------+| Africa | PG | OK to rent to minors. || Amadeus | PG | OK to rent to minors. || Christmas | NR | Use discretion when renting. || Doc | G | OK to rent to minors. || Falcon | NR | Use discretion when renting. || Mash | R | Under 17 requires an adult. || Show | NR | Use discretion when renting. || View | NR | Use discretion when renting. |+-----------+--------+------------------------------+8 rows in set (0.01 sec)*/Drop table DVDs;CREATE TABLE DVDs ( ID SMALLINT NOT NULL AUTO_INCREMENT PRIMARY KEY, Name VARCHAR(60) NOT NULL, NumDisks TINYINT NOT NULL DEFAULT 1, RatingID VARCHAR(4) NOT NULL, StatID CHAR(3) NOT NULL)ENGINE=INNODB;INSERT INTO DVDs (Name, NumDisks, RatingID, StatID)VALUES ('Christmas', 1, 'NR', 's1'), ('Doc', 1, 'G', 's2'), ('Africa', 1, 'PG', 's1'), ('Falcon', 1, 'NR', 's2'), ('Amadeus', 1, 'PG', 's2'), ('Show', 2, 'NR', 's2'), ('View', 1, 'NR', 's1'), ('Mash', 2, 'R', 's2'); SELECT Name, RatingID AS Rating, CASE RatingID WHEN 'R' THEN 'Under 17 requires an adult.' WHEN 'X' THEN 'No one 17 and under.' WHEN 'NR' THEN 'Use discretion when renting.' ELSE 'OK to rent to minors.' END AS PolicyFROM DVDsORDER BY Name; |
mysql case when 条件过滤的更多相关文章
- 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中的条件语句if、case
Mysql中的条件语句在我们对数据进行转换的时候比较有用,这样就不需要创建中转表. IF 函数 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> ...
- mysql多条件过滤查询之mysq高级查询
一.什么是高级查询: ① 多条件的过滤查询 简单说,即拼接sql语句,在sql查询语句之后使用: where 条件1 and/or 条件2 and/or 条件3 - ② 分页查询 二.多条件过滤查询: ...
- Mysql命令-以NULL做where条件过滤时应该写 IS NULL;
以NULL做where条件过滤时应该写 IS NULL;SELECT * FROM pet WHERE death IS NULL; SELECT * FROM pet WHERE death IS ...
- 【MySQL】MySQL中where条件的执行分析
1.问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语法解析.权限检 ...
- MySQL · 性能优化 · 条件下推到物化表
MySQL · 性能优化 · 条件下推到物化表 http://mysql.taobao.org/monthly/2016/07/08/ 背景 MySQL引入了Materialization(物化)这一 ...
- MySQL查询where条件的顺序对查询效率的影响<转>
看到有资料说,where条件的顺序会影响查询的效率,根据的逻辑是: where条件的运行是从右到左的,将选择性强的条件放到最右边,可以先过滤掉大部分的数据(而选择性不强的条件过滤后的结果集仍然很大), ...
- MySQL查询where条件的顺序对查询效率的影响
看到有资料说,where条件的顺序会影响查询的效率,根据的逻辑是: where条件的运行是从右到左的,将选择性强的条件放到最右边,可以先过滤掉大部分的数据(而选择性不强的条件过滤后的结果集仍然很大), ...
- MySQL的简单条件判断语句
在MySQL中条件判断语句常用于数据转换,基于现有数据创建新的数据列,使用场景还是比较多. 基础样式: CASE WHEN`条件`THEN`结果` ELSE`默认结果` END 在同一条判断语句中可以 ...
随机推荐
- apache开源项目--mina
Apache MINA(Multipurpose Infrastructure for Network Applications) 是 Apache 组织一个较新的项目,它为开发高性能和高可用性的网络 ...
- 私有pod简记
http://www.jianshu.com/p/7a82e977281c http://www.jianshu.com/p/ddc2490bff9f 两个工程 1 代码工程 在github上创建一个 ...
- 【转】linux : waitpid函数
原文网址:http://blog.csdn.net/jifengszf/article/details/3067841 [waitpid系统调用] 功能描述: 等待进程改变其状态.所有下面 ...
- 两个android程序间的相互调用(apk互调)
通常我们用到的只是activity之间的互相跳转和调用,很少会用到apk级别的互相调用. 往往在一些应用上会用到,比如一个支付系统,可能会有很多的一系列的程序调用到:彩票系统.订票系统.团购网……全部 ...
- 如何获取数据块结构信息dump
有个pub_department的表,索引为PK_PUB_DEPARTMENT. 1.找到object_id select object_id from dba_objects s where ...
- JS 代码编一个倒时器
有时候在生活中,你需要一个JavaScript倒计时时钟,而不是一个末日装置设备.不管你是否有一次约会,销售.促销.或者游戏,你可以受益于使用原生JavaScript构建一个时钟,而不是拿到一个现成的 ...
- lightoj 1022
直接算即可,特别要注意精度 #include<cstdio> #include<cmath> int main(){ int t, CASE(0); double r; sca ...
- JavaScript中的事件冒泡机制
事件冒泡机制 事件冒泡发生的条件:当为多个嵌套的元素设置了相同的事件处理程序,它们将触发事件冒泡机制.在事件冒泡中,最内部的元素将首先触发其事件,然后是栈内的下一个元素触发该事件,以此类推,直到到达最 ...
- [转]JQuery.Ajax之错误调试帮助信息
下面是Jquery中AJAX参数详细列表: 参数名 类型 描述 url String (默认: 当前页地址) 发送请求的地址. type String (默认: "GET") 请求 ...
- 【Hadoop学习】Super用户以其他用户的名义执行操作
Hadoop版本:2.6.0 本文系从官方文档翻译而来,转载请尊重译者的工作,注明以下链接: http://www.cnblogs.com/zhangningbo/p/4146410.html 简介 ...