Using MySQL case condition to judge the usage of language name

Source: Internet
Author: User

Grammar

The code is as follows Copy Code

Case value when [compare-value] THEN result [when [compare-value] THEN result ...] [ELSE result] When [condition] THEN result [when [condition] THEN result ...] [ELSE result] End

Instance

The code is as follows Copy Code

SELECT case when 10*2=30 THEN ' correct '
When 10*2=40 THEN ' correct '
ELSE ' Should be 10*2=20 '
End;

Complex point

The code is as follows Copy Code
SELECT Case 10*2
When THEN ' correct '
When THEN ' correct '
When THEN ' correct '
End;

Instance

The code is as follows Copy Code
/*
Mysql> SELECT Name, ratingid as Rating,
-> Case Ratingid
-> when ' R ' THEN ' Under requires a adult. '
-> when ' X ' THEN ' No one and under. '
-> when ' NR ' THEN ' with 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 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 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 requires a adult. '
When ' X ' THEN ' No one and under. '
When ' NR ' THEN ' with discretion when renting. '
ELSE ' OK to rent to minors. '
End as Policy
From DVDs
Order BY Name;
Related Article

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.