简体   繁体   English

如何在MySQL中的两个日期范围内计数

[英]How to count at two date range in mysql

I have to export excel by php excel 1 sheet have value on diffrent date range count, 我必须通过php excel导出excel 1张纸,其不同日期范围的计数值,

example:
SELECT count(*) as week_service_times FROM
        (
            SELECT  d.wkdetid,m.eqpid as eqpid,m.assetno as assetno,m.kindname as kindname,m.custname as custname,m.brandname as brandname,e.custname_short as custname_short
            FROM    WKMast as m
            INNER JOIN WKDet as d USING(wkid)
            INNER JOIN DeptCust as e on m.custid = e.custid and m.zoneid = e.zoneid and m.deptid = e.deptid
            LEFT JOIN HandleCode as h on d.causecode = h.causecode and d.hdlcode = h.hdlcode
            LEFT JOIN EQPMast as E on E.eqpid = m.eqpid
            WHERE   m.gpid_p = 'gp02'
            AND     (m.typeid = 'tp01')
            AND     date(m.s_time) BETWEEN '2017-06-01' AND '2017-06-18'
            GROUP BY d.wkdetid
            ORDER BY date(m.s_time)
        ) AS T1
        GROUP BY eqpid    

My date(m_stime) BETWEEN '2017-06-01' AND '2017-06-18' 我的日期(m_stime)在'2017-06-01'和'2017-06-18'之间

But I have to implement other date range like BETWEEN '2017-06-12' AND '2017-06-18' 但是我必须实现其他日期范围,例如BETWEEN'2017-06-12'和'2017-06-18'

second date range must in first data range 第二个日期范围必须在第一个数据范围内

can help me how to implement this in one sql?? 可以帮助我如何在一个SQL中实现这一目标?

export data 汇出资料

Serial NO   2017-06-01 TO 2017-06-18    2017-06-12 TO 2017-06-18
5300077120  5                                 3
5300097135  4                                 2

You could use a MySQL IF with SUM() or COUNT() like below. 您可以将MySQL IF与SUM()或COUNT()一起使用,如下所示。

            COUNT(IF(date >= '2017-06-01' AND date <= '2017-06-18', 1, null)) AS range1,
            COUNT(IF(date >= '2017-06-01' AND date <= '2017-06-12', 1, null)) AS range2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM