zoukankan      html  css  js  c++  java
  • mysql-sql计算

    1.计算某个时间的上一周开始和结束时间

    select
        DATE_FORMAT(xx.start, '%Y-%m-%d') as startTime,
        DATE_FORMAT(ADDDATE(xx.start, 6), '%Y-%m-%d') as
    endTime
    from
    (
    select
        ADDDATE(jan1, (t.week - WEEK(jan1, 5)) * 7 - WEEKDAY(jan1)) as start,
        year,
        WEEK
    from
        (
        select
            MAKEDATE((
            select
                SUBSTR(DATE_SUB(SUBSTR(STR_TO_DATE('2020-11-01', '%Y-%m-%d'), 1, 10), interval 1 WEEK), 1, 4)), 1) as jan1,
            SUBSTR(DATE_SUB(SUBSTR(STR_TO_DATE('2020-11-01', '%Y-%m-%d'), 1, 10), interval 1 WEEK), 1, 4) as year,
            WEEKOFYEAR(DATE_SUB(STR_TO_DATE('2020-11-01', '%Y-%m-%d') , interval 1 WEEK)) as WEEK) t )xx

    2.计算某个时间的上一周开始到结束时间

    SELECT 
        date_format(lastWeek.`timeDay`,'%Y-%m-%d') as 'timeDay' 
    FROM (
        select DATE_SUB(NOW(),interval 13 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 12 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 11 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 10 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 9 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 8 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 7 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 6 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 5 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 4 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 3 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 2 day) as 'timeDay'
        UNION ALL 
        select DATE_SUB(NOW(),interval 1 day) as 'timeDay'
    ) as lastWeek 
    WHERE YEARWEEK(lastWeek.`timeDay`,1) = YEARWEEK(NOW(),1)-1
  • 相关阅读:
    最佳调度问题_分支限界法
    运动员最佳配对问题
    最小重量机器设计问题
    实现银行家算法和先进先出算法_对文件读写数据
    n皇后问题_回溯法
    0-1背包_回溯法
    根据前序、中序、后序遍历还原二叉树
    矩阵连乘问题_动态规划
    最长公共子序列_动态规划
    最优二叉查找树_动态规划
  • 原文地址:https://www.cnblogs.com/linhongwenBlog/p/13934025.html
Copyright © 2011-2022 走看看