简体   繁体   中英

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,

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'

But I have to implement other date range like BETWEEN '2017-06-12' AND '2017-06-18'

second date range must in first data range

can help me how to implement this in one 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.

            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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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