-1

我想要一个在六个月后停用帐户的 SQL 或 PHP 查询。

简单的解决方案是

Select datediff(curdate(),last_date) as diff 

我进入了必须排除周六和周日的复杂情况。

我被困在未来的日期或日期,我必须制作记录的状态 = 0

我在这里找到的答案基于两个日期或日期范围,但我必须计算第二个日期。

4

1 回答 1

1

相当肯定一定有比这更有效的方法,但值得一试(使用 NOW() 作为开始日期):-

SELECT 
    CASE 
        WHEN DAYOFWEEK(NOW()) = 7
            THEN 
                CASE 
                    WHEN DAYOFWEEK(DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 2 DAY)) = 7
                        THEN DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 4 DAY)
                    WHEN DAYOFWEEK(DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 2 DAY)) = 1
                        THEN DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 3 DAY)
                    ELSE 
                        DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 2 DAY)
                END
        WHEN DAYOFWEEK(NOW()) = 1
            THEN
                CASE 
                    WHEN DAYOFWEEK(DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 1 DAY)) = 7
                        THEN DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 3 DAY)
                    WHEN DAYOFWEEK(DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 1 DAY)) = 1
                        THEN DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 2 DAY)
                    ELSE 
                        DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 1 DAY)
                END
        ELSE
            CASE 
                WHEN DAYOFWEEK(DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) DAY)) = 7
                    THEN DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 2 DAY)
                WHEN DAYOFWEEK(DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) DAY)) = 1
                    THEN DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) + 1 DAY)
                ELSE 
                    DATE_ADD(DATE(NOW()), INTERVAL (160 DIV 5) * 7 + (160 % 5) DAY)
            END

    END

天数除以 5 得到周数,mods 除以 5 得到额外天数。如果开始日期是星期六,则增加 2 天,如果是星期日,则增加 1 天,然后增加 7 倍的周数加上额外的天数。然后,如果结果一天是星期六,则再增加 2 天,如果是星期日,则再增加 1 天。

或者以一种有点奇怪的方式:-

SELECT MAX(EndDate)
FROM
(
    SELECT @StartDate:=DATE_ADD(@StartDate, INTERVAL CASE WHEN DAYOFWEEK(@StartDate) = 6 THEN 3 WHEN DAYOFWEEK(@StartDate) = 7 THEN 3 WHEN DAYOFWEEK(@StartDate) = 1 THEN 2 ELSE 1 END DAY) AS EndDate
    FROM
    (
        SELECT units.i + tens.i * 10 + hundreds.i * 100 AS daysAdd
        FROM (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) units,
        (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) tens,
        (SELECT 0 AS i UNION SELECT 1 UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5 UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9) hundreds
        HAVING daysAdd < 160
    ) Sub1
    CROSS JOIN
    (
        SELECT @aCnt:=0, @StartDate:=DATE(NOW())
    ) sub2
) Sub0
于 2013-10-15T09:34:17.610 回答