在mysql中得到当前日期是当月第几周

在mysql中得到当前日期是当月第几周,第1张

代码如下

    select group_date groupDate from (

    SELECT

    @cdate input_date,

    @last_sat := date_add(date_sub(date_sub(@cdate,interval  day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_sub(date_sub(@cdate,interval  day(@cdate)-1 day),interval 1 month),'%w') day) last_sat,

    @this_sat := date_add(date_sub(@cdate,interval  day(@cdate)-1 day), interval 6-date_format(date_sub(@cdate,interval  day(@cdate)-1 day),'%w') day) this_sat,

    @next_sat := date_add(date_add(date_sub(@cdate,interval  day(@cdate)-1 day),interval 1 month), interval 6-date_format(date_add(date_sub(@cdate,interval  day(@cdate)-1 day),interval 1 month),'%w') day) next_sat,

    @last_point := if(day(@last_sat)-3>0,date_sub(@last_sat,interval 7 day),@last_sat) last_point,

    @this_point := if(day(@this_sat)-3>0,date_sub(@this_sat,interval 7 day),@this_sat) this_point,

    @next_point := if(day(@next_sat)-3>0,date_sub(@next_sat,interval 7 day),@next_sat) next_point ,

    case

    when @cdate >= @last_point and @cdate < @this_point then

    concat(

    'W',

    datediff(@cdate,@last_point) div 7 + 1, '/',

    year(@cdate),'-',

    if(month(@last_sat)>=10,month(@last_sat),concat('0',month(@last_sat)))

    )

    when @cdate >=@this_point and @cdate < @next_point then

    concat(

    'W',

    datediff(@cdate,@this_point) div 7 + 1,'/',

    year(@cdate),'-',

    if(month(@this_sat)>=10,month(@this_sat),concat('0',month(@this_sat))))

    else

    concat(

    'W',

    datediff(@cdate,@next_point) div 7 + 1,'/',

    year(@cdate),'-',if(month(@next_sat)>=10,month(@next_sat),concat('0',month(@next_sat))))

    end

    group_date

    from  (SELECT @cdate := #{inputDate}) as gd

    );

select * from 表名 where 时间字段名>=date_sub(curdate(),interval 7 day)

近一周的数据

select DATE_FORMAT(d.dateType,'%Y%u') as '年份周数',d.shuzhi,d.type

from 'date_dealsheetpage'd

order by d.dateType

查询每周

select SUBSTRING(d.dateType, 1,7) as '年份月份',d.shuzhi,d.type

from 'date_dealsheetpage'd

查询每月

select DATE_FORMAT(order_Date,'%Y-%m-%d') orderDate from order

group by orderDate

order by orderDate DESC

即可!


欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/7409393.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2023-04-05
下一篇 2023-04-05

发表评论

登录后才能评论

评论列表(0条)

保存