mysql 查询无则为0_mysql查询当月每一天数据,如果当天无数据,则为0

查询当月1号至31号每天的数据,

SELECT

date_add(DATE_ADD(curdate(), INTERVAL - DAY(curdate()) + 2 DAY),

INTERVAL (cast( help_topic_id AS signed INTEGER ) - 1 ) DAY

) DAY

FROM

mysql.help_topic

WHERE

help_topic_id < DAY ( last_day( curdate( ) ) )

ORDER BY

help_topic_id

查询 1号至当前日期每天的数据

SELECT

date_add( date_sub( '2020-08-01 00:00:00.0', INTERVAL 1 DAY ), INTERVAL ( cast( help_topic_id AS signed INTEGER ) + 1 ) DAY ) 'day'

FROM

mysql.help_topic

WHERE

help_topic_id < DATEDIFF( NOW( ), date_sub( '2020-08-01 00:00:00.0', INTERVAL 1 DAY ) )

ORDER BY

help_topic_id

SELECT DATE_FORMAT(leftdata.day,'%Y-%m-%d'),IFNULL(rightdata.countNumber,'0') FROM

(

SELECT

date_add(DATE_ADD(curdate(), INTERVAL - DAY(curdate()) + 2 DAY),

INTERVAL (cast( help_topic_id AS signed INTEGER ) - 1 ) DAY

) DAY

FROM

mysql.help_topic

WHERE

help_topic_id < DAY ( last_day( curdate( ) ) )

ORDER BY

help_topic_id

) as leftdata

LEFT JOIN

(

SELECT

DATE_FORMAT( create_time, '%Y-%m-%d' ) AS dateTime,

COUNT( 1 ) AS countNumber

FROM

t_mscm_sales

GROUP BY

DATE_FORMAT( create_time, '%Y-%m-%d' )

) as rightdata

ON

DATE_FORMAT(leftdata.day,'%Y-%m-%d') = rightdata.dateTime

----------------------------------------------------------------------------------------

查询当月1号至当前日期的所有数据,如果当天无数据则为0

SELECT

( CASE WHEN b.c_id IS NULL THEN 0 ELSE COUNT( * ) END ) count,

a.DAY

FROM

(

SELECT

date_add( date_sub( '2020-08-01', INTERVAL 1 DAY ), INTERVAL ( cast( help_topic_id AS signed INTEGER ) + 1 ) DAY ) DAY

FROM

mysql.help_topic

WHERE

help_topic_id < DATEDIFF( NOW( ), date_sub( '2020-08-01', INTERVAL 1 DAY ) )

ORDER BY

help_topic_id

) a

LEFT JOIN t_mscm_sales b ON DATE( b.update_time ) = a.DAY

AND

b.n_states = 6

GROUP BY

a.DAY

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值