代码语言
.
CSharp
.
JS
Java
Asp.Net
C
MSSQL
PHP
Css
PLSQL
Python
Shell
EBS
ASP
Perl
ObjC
VB.Net
VBS
MYSQL
GO
Delphi
AS
DB2
Domino
Rails
ActionScript
Scala
代码分类
文件
系统
字符串
数据库
网络相关
图形/GUI
多媒体
算法
游戏
Jquery
Extjs
Android
HTML5
菜单
网页交互
WinForm
控件
企业应用
安全与加密
脚本/批处理
开放平台
其它
【
MYSQL
】
CASE WHEN 语句
作者:
Dezai.CN
/ 发布于
2011/6/15
/
1778
<div>使用CASE WHEN进行字符串替换处理</div> <div></div> <div>/*</div> <div>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)</div> <div>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)</div> <div> */ 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;</div> <div> 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');</div> <div>select * from sales;</div> <div> SELECT name AS Name, CASE category WHEN "Holiday" THEN "Seasonal" WHEN "Profession" THEN "Bi_annual" WHEN "Literary" THEN "Random" END AS "Pattern" FROM sales; </div> <div></div> <div></div> <div>在SELECT查询中使用CASE WHEN</div> <div></div> <div>/* 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)</div> <div> */</div> <div>Drop table DVDs;</div> <div>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;</div> <div>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'); </div> 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; </div> <div></div> <div></div>
试试其它关键字
同语言下
.
MySQL按日期分组统计(按天统计,按月统计)
.
mysql 获取某个库下面所有表的所有数据(
.
查看表当前自增值
.
查看碎片较多的表
.
查看数据表较大的表
.
查看非InnoDB引擎表
.
根据某一字段值按照某一特定符转化为列数据
.
按时间段分组查询当天小时内数据
.
mysql json 使用 类型 查询 函数
.
按天数据统计
可能有用的
.
MySQL按日期分组统计(按天统计,按月统计)
.
mysql 获取某个库下面所有表的所有数据(
.
查看表当前自增值
.
查看碎片较多的表
.
查看数据表较大的表
.
查看非InnoDB引擎表
.
根据某一字段值按照某一特定符转化为列数据
.
按时间段分组查询当天小时内数据
.
mysql json 使用 类型 查询 函数
.
按天数据统计
Dezai.CN
贡献的其它代码
(
4037
)
.
多线程Socket服务器模块
.
生成随机密码
.
清除浮动样式
.
弹出窗口居中
.
抓取url的函数
.
使用base HTTP验证
.
div模拟iframe嵌入效果
.
通过header转向的方法
.
Session操作类
.
执行sqlite输入插入操作后获得自动编号的ID
Copyright © 2004 - 2024 dezai.cn. All Rights Reserved
站长博客
粤ICP备13059550号-3