mysql 获得年月日的数值_mysql函数date_format统计刷选按年月日统计的数据

/*原型*/

SELECT

count(did) AS sum,

date_format(releasetime, '%Y-%m-%d') AS releasetime

FROM hengtu_demand

WHERE 1

GROUP BY

date_format(releasetime, '%Y-%m-%d')

/*按年分组 @参数 需求id sid=1*/

SELECT

b.sid,

b.`name` as sname,

count(did) AS value,

date_format(releasetime, '%Y') AS name

FROM

hengtu_demand a

LEFT JOIN hengtu_service b ON a.sid = b.sid

WHERE

b.`status` = 1

AND b.sid = 1

GROUP BY

date_format(a.releasetime, '%Y')

/*按月分组 @参数 需求id sid=1 @参数 年份 2014*/

SELECT

b.sid,

b.`name` as sname,

count(did) AS value,

date_format(releasetime, '%Y') AS year,

date_format(releasetime, '%m') AS month,

date_format(releasetime, '%m') AS name

FROM

hengtu_demand a

LEFT JOIN hengtu_service b ON a.sid = b.sid

WHERE

b.`status` = 1

AND b.sid = 1

GROUP BY

date_format(a.releasetime, '2014-%m')

/*按日分组 @参数 需求id sid=1 @参数 年份 2014 @参数 月份 1月*/

SELECT

b.sid,

b.`name` as sname,

count(did) AS value,

date_format(releasetime, '2014') AS year,

date_format(releasetime, '11') AS month,

date_format(releasetime, '%d') AS day,

date_format(releasetime, '%d') AS name

FROM

hengtu_demand a

LEFT JOIN hengtu_service b ON a.sid = b.sid

WHERE

b.`status` = 1

AND b.sid = 1

GROUP BY

date_format(a.releasetime, '2014-11-%d')

/**按年月查询加入where条件 正确标准语句*/

SELECT

b.sid,

b.`name` AS sname,

count(did) AS value,

date_format(releasetime, '%Y') AS year,

date_format(releasetime, '%m') AS month,

date_format(releasetime, '%d') AS day,

date_format(releasetime, '%d') AS name

FROM

hengtu_demand a

LEFT JOIN hengtu_service b ON a.sid = b.sid

WHERE

b.`status` = 1

AND b.sid = ".$sid."

AND date_format(releasetime, '%m') = ".$month."

GROUP BY

date_format(

a.releasetime,

'".$year."-".$month."-%d'

)

/*

*如果数据库时间是以时间戳格式储存的就使用form_unixtime函数

*/

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值