Mysql when case condition judgment language name usage

Source: Internet
Author: User

The default Return Value Type of a CASE expression is the compatible set type of any return value, but the specific situation depends on the context. If it is used in a string context, a result flavor string is returned. If it is used in the numeric context, the returned result is a decimal value, real value, or integer value.

Syntax

The Code is as follows: Copy code

CASE value WHEN [compare-value] THEN result [WHEN [compare-value] THEN result...] [ELSE result] end case 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 '30 correct'
WHEN 10*2 = 40 THEN '40 correct'
ELSE 'should be 10*2 = 20'
END;

Complexity

 

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

Instance

 

The Code is as follows: Copy code
/*
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.