The use of case-then-else end in MYSQL

Source: Internet
Author: User

Explain:

SELECT
Case-------------If
When the sex= ' 1 ' then ' Male '-------------sex= ' 1 ', the return value ' male '
When the sex= ' 2 ' then ' female '-------------sex= ' 2 ', the return value ' female '
else 0 return ' other '-------------other
End-------------Ended
From Sys_user--------Overall understanding: In the Sys_user table if sex= ' 1 ', then return the 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

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.