mysql 7天自动拒单功能,mysql查询最近7天的数据,没有数据自动补0
select DATE( createtime) date , createtime, count(1) as count ? from? order表? where DATEDIFF( now(), createtime)<=7
group by date ;
12 月九號沒有;
給他在關聯一張日期的表;
--生成從今天開始完整的7天日期
DECLARE @LastSevenDaytable
(
day date
)
DECLARE @StartDaydate = GETDATE();
DECLARE @InsertDaydate = @StartDay;
WHILE DATEDIFF(DAY,@InsertDay,@StartDay) < 7
BEGIN
INSERT INTO @LastSevenDay
VALUES ( @InsertDay)
SET @InsertDay = DATEADD(day,-1,@InsertDay)
END
--生成最后結果
SELECT DATEPART(day,lsd.day)AS '日期' ,COUNT(orders.id)AS '訂單量'
FROM @LastSevenDayAS lsd
LEFT JOIN orders
ON lsd.day =CONVERT(varchar(10),orders.ordertime,120)
GROUP BY lsd.day
但是呢, 我們不想這么麻煩;? 一個SQL 就可以;
套一層 :
select a.click_date,ifnull(b.count,0) as count
from (
SELECT curdate() as click_date
union all
SELECT date_sub(curdate(), interval 1 day) as click_date
union all
SELECT date_sub(curdate(), interval 2 day) as click_date
union all
SELECT date_sub(curdate(), interval 3 day) as click_date
union all
SELECT date_sub(curdate(), interval 4 day) as click_date
union all
SELECT date_sub(curdate(), interval 5 day) as click_date
union all
SELECT date_sub(curdate(), interval 6 day) as click_date
) a left join (
-- 原來的sql
select DATE( createtime) date , createtime, count(1) as count ? from order表? where DATEDIFF( now(), createtime)<=7
group by date
) b on a.click_date = b.date;
參看自:?https://blog.csdn.net/ljxbbss/article/details/78028424
總結
以上是生活随笔為你收集整理的mysql 7天自动拒单功能,mysql查询最近7天的数据,没有数据自动补0的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 海关拍卖总价33万元游戏主机:上千台水货
- 下一篇: 涨价太离谱 长安汽车不卖了!奔奔E-St