MySQL CASEWHEN statement instructions

Source: Internet
Author: User
MySQL CASEWHEN statement usage instructions. For more information, see.

MySQL case when statement instructions. For more information, see.

Use case when for string replacement
The Code is as follows:
/*
Mysql> select * from sales;
+ ----- + ------------ + -------- + ------ + ------------ +
| Num | name | winter | spring | summer | fall | category |
+ ----- + ------------ + -------- + ------ + ------------ +
| 1 | Java | 1067 | 200 | 150 | 267 | Holiday |
| 2 | C | 970 | 770 | 531 | 486 | sion |
| 3 | JavaScript | 53 | 13 | 21 | 856 | Literary |
| 4 | SQL | 782 | 357 | 168 | 250 | sion |
| 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 | sion |
+ ----- + ------------ + -------- + ------ + ------------ +
9 rows in set (0.01 sec)
Mysql> SELECT name AS Name,
-> CASE category
-> WHEN "Holiday" THEN "Seasonal"
-> WHEN "Sion" 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, 'sale sion ');
Insert into sales value (3, 'javascript ', 21,856, 'litery ');
Insert into sales value (4, 'SQL', 782,357,168,250, 'analysion ');
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, 'plusion ');
Select * from sales;
SELECT name AS Name,
CASE category
WHEN "Holiday" THEN "Seasonal"
WHEN "Sion" THEN "Bi_annual"
WHEN "Literary" THEN "Random" end as "Pattern"
FROM sales;

Simple statement
The Code is as follows:
Select case when 10*2 = 30 THEN '30 correct'
WHEN 10*2 = 40 THEN '40 correct'
ELSE 'should be 10*2 = 20'
END;

Multiple Expressions
The Code is as follows:
Select case 10*2
WHEN 20 THEN '20 correct'
WHEN 30 THEN '30 correct'
WHEN 40 THEN '40 correct'
END;

Use case when in SELECT query
The Code is as follows:
/*
Mysql> SELECT Name, RatingID AS Rating,
-> CASE RatingID
-> WHEN 'r'then' Under 17 requires an adult .'
-> WHEN 'X' No one 17 and under .'
-> WHEN 'nr 'then' Use discretion when refreshing .'
-> 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 refreshing. |
| Doc | G | OK to rent to minors. |
| Falcon | NR | Use discretion when authentication ing. |
| Mash | R | Under 17 requires an adult. |
| Show | NR | Use discretion when refreshing. |
| View | NR | Use discretion when refreshing. |
+ ----------- + -------- + ------------------------------ +
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' No one 17 and under .'
WHEN 'nr 'then' Use discretion when processing ing .'
ELSE 'OK to rent to minors .'
End as Policy
FROM DVDs
Order by Name;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.