Usage of the key word in MYSQL when the then else end

Source: Internet
Author: User

Explain:

1 SELECT            2      Case                   -------------If3      whenSex='1'  Then 'male' -------------sex= ' 1 ', the return value ' male '4      whenSex='2'  Then 'female' -------------sex= ' 2 ', the return value ' female '5     Else 'other'                 -------------Other return ' other '6     End                    -------------End7  fromSys_user--------Overall Understanding: In the Sys_user table if sex= ' 1 ', then the return value ' male ' if sex= ' 2 ', then return the value ' female ' otherwise return ' other '

---usage one:

1 SELECT 2              Case  whenState= '1'  Then 'Success' 3                   whenState= '2'  Then 'failed'4             ELSE 'other' END  5              fromSys_scheduler

---usage II:

1 SELECT State2              Case  when '1'  Then 'Success' 3                   when '2'  Then 'failed'4             ELSE 'other' END  5              fromSys_scheduler

Example:

1 have employee table Empinfo2 ( 3Fempnovarchar2(Ten) not NULLPK,4Fempnamevarchar2( -) not NULL, 5Fage Number  not NULL, 6Fsalary Number  not NULL 7 ); 8 if the amount of data is about 10 million; write a SQL that you think is most efficient, and use a SQL to calculate the following four types of people:9Fsalary>9999  andFage>  * TenFsalary>9999  andFage<  *  OneFsalary<9999  andFage>  *  AFsalary<9999  andFage<  *  - the number of employees per type; - Select sum( Case  whenFsalary> 9999  andFage>  * the  Then 1 - Else0End) as"Fsalary>9999_fage> *", - sum( Case  whenFsalary> 9999  andFage<  * -  Then 1 + Else 0 - End) as"Fsalary>9999_fage< *", + sum( Case  whenFsalary< 9999  andFage>  * A  Then 1 at Else 0 - End) as"Fsalary<9999_fage> *", - sum( Case  whenFsalary< 9999  andFage<  * -  Then 1 - Else 0 - End) as"Fsalary<9999_fage< *" in  fromEmpinfo;
View Code

Usage of the key word in MYSQL when the then else end

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.