MySQL with conditional fetch count record number

Source: Internet
Author: User

Reference Method Three:

Count Sub_type= ' Refund_fee ' records:

Method one. Select count (sub_type) from T where t.sub_type= ' refund_fee ';

Method two . Select sum (if (b.sub_type= ' Refund_fee ', 1,0)) from T;

Method Three. Select count (b.sub_type= ' refund_fee ' or null) from T;

Explain:

When select B.sub_type= ' Refund_fee ':

If Sub_type is Refund_fee, return 1,

is not empty and is not refund_fee otherwise returns 0,

Empty returns NULL.

So b.sub_type= ' refund_fee ' or null returns only Sub_type= ' Refund_fee ', and the rest returns NULL, and count (column name) is not counted as the number of NULL

Note: Count (*) will also count the number of NULL

Project SQL

    SELECT Subjectname,doctorname,count (1) as Sumnum,
count (overtimes>0 or null) as Overnum //Count overtimes>0 only
From Ht_personstream
WHERE 1=1
<if test= "subjectid!=" and subjectid!= ' null ' >
and Subjectid = #{subjectid}
</if>
<if test= "starttime!=" and starttime!= ' null ' >
date_format (ENDTIME, '%y-%m-%d ')>= #{starttime}]]> //mysql date formatting
</if>
<if test= "endtime!=" and endtime!= ' null ' >
<! [Cdata[and Date_format (ENDTIME, '%y-%m-%d ') <= #{endtime}]]>
</if>
GROUP by Doctorid,subjectid

MySQL with conditional fetch count record number

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.