Mysql _ case when for string replacement

Source: Internet
Author: User

Use mysql _ case when to replace strings. Use case when to replace strings.

03 mysql> select * from sales; 04 + ----- + ------------ + -------- + ------ + ------------ + 05 | num | name | winter | spring | summer | fall | category | 06 + ----- + ------------ + -------- + -------- + ------ + ------------ + 07 | 1 | Java | 1067 | 200 | 150 | 267 | Holiday | 08 | 2 | C | 970 | 770 | 531 | 486 | sion | 09 | 3 | JavaScript | 53 | 13 | 21 | 856 | Literary | 10 | 4 | SQL | 782 | 357 | 168 | sion | 11 | 5 | Oracle | 250 | 795 | 367 | 284 | Holiday | 12 | 6 | MySQL | 953 | 582 | 336 | 489 | Literary | 13 | 7 | Cplus | 752 | 657 | 259 | 478 | Literary | 14 | 8 | Python | 67 | 23 | 83 | 543 | Holiday | 15 | 9 | PHP | 673 | 48 | 625 | 52 | sion | 16 + ----- + ------------ + -------- + -------- + ------ + ------------ + 17 9 rows in set (0.01 sec) 18 19 mysql> SELECT name AS Name, 20-> CASE category21-> WHEN "Holiday" THEN "Seasonal" // Replace the Holiday value in the sales table field category with seasonal22-> WHEN "sion Sion" THEN "Bi_annual" 23-> WHEN "Literary" THEN "Random" end as "Pattern" // the query result is named AS a new field pattern24-> FROM sales; 25 + ------------ + ----------- + 26 | Name | Pattern | 27 + ------------ + ----------- + 28 | Java | Seasonal | 29 | C | Bi_annual | 30 | JavaScript | Random | 31 | SQL | bi_annual | 32 | Oracle | Seasonal | 33 | MySQL | Random | 34 | Cplus | Random | 35 | Python | Seasonal | 36 | PHP | Bi_annual | 37 + ------------ + ----------- + 38 9 rows in set (0.00 sec) 39 40 41 */42 Drop table sales; 43 44 create table sales (45 num mediumint not null AUTO_INCREMENT, 46 name CHAR (20), 47 winter INT, 48 spring INT, 49 summer INT, 50 fall INT, 51 category CHAR (13), 52 primary key (num) 53) type = MyISAM; 54 55 56 insert into sales value (1, 'java', 1067,200,150,267, 'holiday '); 57 insert into sales value (2, 'C', 970,770,531,486, 'sale sion'); 58 insert into sales value (3, 'javascript ', 21,856, 782,357,168,250, 'literary'); 59 insert into sales value (4, 'SQL', 'sale sion'); 60 insert into sales value (5, 'oracle ', 589,795,367,284, 'holiday'); 61 insert into sales value (6, 'mysql', 953,582,336,489, 'literary '); 62 insert into sales value (7, 'cplus ', 752,657,259,478, 'literary'); 63 insert into sales value (8, 'python', 83,543, 'holiday '); 64 insert into sales value (9, 'php', 673,48, 625,52, 'extension'); 65 66 select * from sales; 67 68 69 SELECT name AS Name, 70 CASE category71 WHEN "Holiday" THEN "Seasonal" 72 WHEN "Sion" THEN "Bi_annual" 73 WHEN "Literary" THEN "Random" end as "Pattern" 74 FROM sales; SELECT num, name AS Name, case categorywhen "Holiday" then "1111" # Replace the Holiday in the categroy field with 1111 WHEN "Sion" THEN "2222" # Replace the Holiday in the categroy field with 2222 WHEN "Literary" THEN "3333" # Replace the Holiday in the categroy field with 3333END AS "new name title" # rename categroy AS 'new name title' FROM sales;

 

 

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.