[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 category
WHEN "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 Policy
FROM DVDs
ORDER BY Name;

mysql case when 条件过滤的更多相关文章

  1. mysql case....when条件

    oracle的写法SELECT decode(ttype,1,’a',2,’b',3,’c',’d') FROM taba 可以在mysql里写成SELECT if(ttype=1, 'a',if(t ...

  2. Mysql中的条件语句if、case

    Mysql中的条件语句在我们对数据进行转换的时候比较有用,这样就不需要创建中转表. IF 函数 IF(expr1,expr2,expr3) 如果 expr1 是TRUE (expr1 <> ...

  3. mysql多条件过滤查询之mysq高级查询

    一.什么是高级查询: ① 多条件的过滤查询 简单说,即拼接sql语句,在sql查询语句之后使用: where 条件1 and/or 条件2 and/or 条件3 - ② 分页查询 二.多条件过滤查询: ...

  4. Mysql命令-以NULL做where条件过滤时应该写 IS NULL;

    以NULL做where条件过滤时应该写 IS NULL;SELECT * FROM pet WHERE death IS NULL; SELECT * FROM pet WHERE death IS ...

  5. 【MySQL】MySQL中where条件的执行分析

    1.问题描述 一条SQL,在数据库中是如何执行的呢?相信很多人都会对这个问题比较感兴趣.当然,要完整描述一条SQL在数据库中的生命周期,这是一个非常巨大的问题,涵盖了SQL的词法解析.语法解析.权限检 ...

  6. MySQL · 性能优化 · 条件下推到物化表

    MySQL · 性能优化 · 条件下推到物化表 http://mysql.taobao.org/monthly/2016/07/08/ 背景 MySQL引入了Materialization(物化)这一 ...

  7. MySQL查询where条件的顺序对查询效率的影响<转>

    看到有资料说,where条件的顺序会影响查询的效率,根据的逻辑是: where条件的运行是从右到左的,将选择性强的条件放到最右边,可以先过滤掉大部分的数据(而选择性不强的条件过滤后的结果集仍然很大), ...

  8. MySQL查询where条件的顺序对查询效率的影响

    看到有资料说,where条件的顺序会影响查询的效率,根据的逻辑是: where条件的运行是从右到左的,将选择性强的条件放到最右边,可以先过滤掉大部分的数据(而选择性不强的条件过滤后的结果集仍然很大), ...

  9. MySQL的简单条件判断语句

    在MySQL中条件判断语句常用于数据转换,基于现有数据创建新的数据列,使用场景还是比较多. 基础样式: CASE WHEN`条件`THEN`结果` ELSE`默认结果` END 在同一条判断语句中可以 ...

随机推荐

  1. DbgPrint输出格式 Unicodestring

    DbgPrint输出格式 Unicodestring  1) 直接打印字符串. DbgPrint("Hello World!"); 2) 空结尾的字符串,你可以用普通得C语法表示字 ...

  2. UVA 568 Just the Facts (水)

    题意: 求一个数n的阶乘,其往后数第1个不是0的数字是多少. 思路: [1,n]逐个乘,出现后缀0就过滤掉,比如12300就变成123,继续算下去.为解决爆long long问题,将其余一个数mod, ...

  3. Oracle表与索引的分析及索引重建

    1.分析表与索引(analyze 不会重建索引)   analyze table tablename compute statistics 等同于 analyze table tablename co ...

  4. Spring学习之AOP

    Spring-AOP(Aspect-orented programming) 在业务流程中插入与业务无关的逻辑,这样的逻辑称为Cross-cutting concerns,将Crossing-cutt ...

  5. Android设计模式之命令模式、策略模式、模板方法模式

    命令模式是其它很多行为型模式的基础模式.策略模式是命令模式的一个特例,而策略模式又和模板方法模式都是算法替换的实现,只不过替换的方式不同.下面来谈谈这三个模式. 命令模式 将一个请求封装为一个对象,从 ...

  6. Android-取消GridView/ListView item被点击时的效果

    方法一,在控件被初始化的时候设置 gridView.setSelector(new ColorDrawable(Color.TRANSPARENT)); listView.setSelector(ne ...

  7. button捕捉回车键

    为了给一个页面设置回车默认触发的按钮功能,我浏览了IE上的诸多方法,有的言不达意,有的读不懂,后来把高手的一段代码改造后,形成了一段代码,使这个问题的解决变得非常简章,有兴趣的朋友不妨一试.<s ...

  8. [Everyday Mathematics]20150114

    设 $a_0$, $d$ 给定, $a_k=a_0+kd$, $k=0,1,\cdots,n$. 试求如下 $n+1$ 阶行列式的值: $$\bex \sev{\ba{ccccc} a_0&a ...

  9. replace() MySQL批量替换指定字段字符串

    mysql replace实例说明: UPDATE tb1 SET f1=REPLACE(f1, 'abc', 'def'); REPLACE(str,from_str,to_str) 在字符串 st ...

  10. Matlab编程实例(3) 函数向左或向右平移N点 左移右移

    %函数移动 close all; clear all; dir=input('请输入平移方向,“1”为向左,“2”为向右'); if dir~=1&&dir~=2;%输入控制    e ...