Easy to understand, directly on the example:
SELECT
Case-------------if
When sex= ' 1 ' Then ' Men '-------------sex= ' 1 ', then return value ' Male '
When sex= ' 2 ' Then ' Women '-------------sex= ' 2 ', then the return value ' female '
else 0-------------other return to ' other '
End-------------End
From Sys_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:
SELECT
Case is state = ' 1 ' Then ' success '
When state = ' 2 ' Then ' failed '
Else ' other ' END
From Sys_scheduler
---Usage II:
SELECT State
Case "1 ' Then ' success '
When ' 2 ' then ' failed '
Else ' other ' END
From Sys_scheduler
Liezi:
have employee table Empinfo
(
Fempno VARCHAR2 (TEN) NOT NULL PK,
Fempname varchar2 () NOT NULL,
FAGE number is not NULL,
Fsalary number NOT NULL
);
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:
fsalary>9999 and Fage > 35
fsalary>9999 and Fage < 35
Fsalary <9999 and Fage > 35
Fsalary <9999 and Fage < 35
The number of employees per type;
Select sum (case when fsalary > 9999 and Fage > 35
Then 1
else 0end) as "fsalary>9999_fage>35",
SUM (case when fsalary > 9999 and Fage < 35
Then 1
else 0
End) as "fsalary>9999_fage<35",
SUM (case when Fsalary < 9999 and Fage > 35
Then 1
else 0
End) as "fsalary<9999_fage>35",
SUM (case when Fsalary < 9999 and Fage < 35
Then 1
else 0
End) as "fsalary<9999_fage<35"
From Empinfo;
The use of case-then-else end in MYSQL