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