Use case when for string replacement
Copy codeThe 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 statementCopy codeThe 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 ExpressionsCopy codeThe 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 queryCopy codeThe 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;