Statistical query using one SQL statement _ MySQL

Source: Internet
Author: User
One SQL statement for statistics query bitsCN.com

One SQL statement for statistical query

When programmers make the following statistics, they now provide two implementation solutions:

Solution 1: Use SEKECT CASE WHEN

Explain select count (*) AS 'total', count (CASE oup. status WHEN '1' THEN oup. id END) AS 'unbound ', count (CASE oup. status WHEN '2' THEN oup. id END) AS 'bound', count (CASE oup. status WHEN '3' THEN oup. id END) AS 'freeze 'FROM lab_org_uc_passport oup

Display result: (display by row)

Solution 2:

SELECT count (*) AS 'Total' FROM lab_org_uc_passport oup union all select count (*) AS 'not bound' FROM lab_org_uc_passport oup WHERE oup. status = '1' union all select count (*) AS 'not bound' FROM lab_org_uc_passport oup WHERE oup. status = '2' union all select count (*) AS 'not bound' FROM lab_org_uc_passport oup WHERE oup. status = '3'

Display result (by column)

34

3

10

21

BitsCN.com

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.