mysql查詢日期補全(連續)方法

2021-08-02 06:06:49 字數 2349 閱讀 5478

select

od.date,

od.ordercount

from

(select

date_format(order_time,'%y-%m-%d') date,

count(*) ordercount

from

order

where

order_time>'開始時間'

and order_time<'結束時間'

group

by date_format(order_time,'%y-%m-%d')

union (select

0, '2017-03-01')

union (select

0, '2017-03-02')

union (select

0, '2017-03-03')

union (select

0, '2017-03-04')

union (select

0, '2017-03-05')

union (select

0, '2017-03-06')

union (select

0, '2017-03-07')

union (select

0, '2017-03-08')

-- 很多個union ......

) as od

group

by od.date

order

by od.date

asc如果有日期表那就用聯表就行了,這裡是不聯表的方法,在程式上做處理後拼接到sql上

public function

getlaidianlaifang

($proj_ids

, $start_date

, $end_date

, $gjfs

, $type

)$where

= " and proj_id in ('"

.implode(

"','"

, $proj_ids

)."')"

;$rs

= $this

->

dealdate

($type

, $start_date

, $end_date);

$group

= $rs

['group'];

$union

= $rs

['union'];

$sql

= "select d.gj_cst_sum,d.date_type from (select

count

(distinct opp_id) as gj_cst_sum,

as date_type from s_opp2gjjl where gjfs =

and gj_date between :start_date and :end_date

group by )

as d group by d.date_type order by d.date_type asc"

;return

$this

->

kfsdb

->

createcommand

($sql, [

':start_date'

=>

$start_date

, ':end_date'

=>

$end_date

.' 23:59:59'

])->

queryall();

}

/**

* 處理時間,把缺的補上

* */

private function

dealdate

($type

, $start_date

, $end_date

, $proj

= null

) )";}

}elseif

($type

== 3)'

)";}}

elseif

($type

== 4

) )";}

}elseif

($type

== 5

) )";}

}return

['group'

=>

$group

, 'union'

=>

$union];

}

MySQL 補全缺失的日期

將缺失的日期建立出來 建立乙個表用來儲存0 9的數字 insert into num i values 0 1 2 3 4 5 6 7 8 9 生成0 9的數字,方便以後計算時間 create table ifnot exists calendar datelist date 生成乙個儲存日期的表,...

mysql日期查詢 mysql 查詢日期

檢視本月資料 select from content publish where date format publish time,y m date format date sub curdate interval 0 month y m 檢視上個月資料 select from content pu...

mysql 日期查詢 Mysql日期查詢list

當前week的第一天 select date sub curdate interval weekday curdate 1 day 當前week的最後一天 select date sub curdate interval weekday curdate 5 day 前一week的第一天 select...