1
A
回答
1
一種方法是有物化日/日期表。但是,用於構建此物化表的相同方法可以直接用於查詢。我表現出一對夫婦[平日]計算的,但你可以用同樣的方法來查詢有關週末兩天(雙休日全天值爲5和6):
直接單查詢例如:
SELECT day
, WEEKDAY(day) AS wkday
FROM (
SELECT FROM_DAYS(d.day1+v1.result) AS day
FROM (SELECT TO_DAYS(DATE('2000-01-01')) AS day1
, TO_DAYS(DATE('2021-01-01')) AS day2
) AS d
JOIN (
SELECT v1.num+v2.num+v3.num+v4.num AS result
FROM (
SELECT 1 AS num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0
) AS v1
JOIN (
SELECT 10 AS num UNION SELECT 20 UNION SELECT 30 UNION SELECT 40 UNION SELECT 50
UNION SELECT 60 UNION SELECT 70 UNION SELECT 80 UNION SELECT 90 UNION SELECT 00
) AS v2
JOIN (
SELECT 100 AS num UNION SELECT 200 UNION SELECT 300 UNION SELECT 400 UNION SELECT 500
UNION SELECT 600 UNION SELECT 700 UNION SELECT 800 UNION SELECT 900 UNION SELECT 000
) AS v3
JOIN (
SELECT 1000 AS num UNION SELECT 2000 UNION SELECT 3000 UNION SELECT 4000 UNION SELECT 5000
UNION SELECT 6000 UNION SELECT 7000 UNION SELECT 8000 UNION SELECT 9000 UNION SELECT 0000
) AS v4
) v1
WHERE v1.result < (d.day2-d.day1)
) AS days
WHERE WEEKDAY(day) < 5
LIMIT 10
;
USE test;
DROP TABLE IF EXISTS days;
CREATE TABLE days (
day date PRIMARY KEY
) ENGINE = InnoDB;
INSERT INTO days
SELECT FROM_DAYS(d.day1+v1.result)
FROM (SELECT TO_DAYS(DATE('2000-01-01')) AS day1
, TO_DAYS(DATE('2021-01-01')) AS day2
) AS d
JOIN (
SELECT v1.num+v2.num+v3.num+v4.num AS result
FROM (
SELECT 1 AS num UNION SELECT 2 UNION SELECT 3 UNION SELECT 4 UNION SELECT 5
UNION SELECT 6 UNION SELECT 7 UNION SELECT 8 UNION SELECT 9 UNION SELECT 0
) AS v1
JOIN (
SELECT 10 AS num UNION SELECT 20 UNION SELECT 30 UNION SELECT 40 UNION SELECT 50
UNION SELECT 60 UNION SELECT 70 UNION SELECT 80 UNION SELECT 90 UNION SELECT 00
) AS v2
JOIN (
SELECT 100 AS num UNION SELECT 200 UNION SELECT 300 UNION SELECT 400 UNION SELECT 500
UNION SELECT 600 UNION SELECT 700 UNION SELECT 800 UNION SELECT 900 UNION SELECT 000
) AS v3
JOIN (
SELECT 1000 AS num UNION SELECT 2000 UNION SELECT 3000 UNION SELECT 4000 UNION SELECT 5000
UNION SELECT 6000 UNION SELECT 7000 UNION SELECT 8000 UNION SELECT 9000 UNION SELECT 0000
) AS v4
) v1
WHERE v1.result < (d.day2-d.day1)
;
SELECT *
FROM days
ORDER BY day
LIMIT 10
;
SELECT COUNT(*) FROM days;
SELECT MIN(day), MAX(day) FROM days;
SELECT day, WEEKDAY(day) FROM days LIMIT 6;
SELECT day, WEEKDAY(day) AS wkday FROM days WHERE WEEKDAY(day) < 5 LIMIT 6;
SELECT COUNT(*), MIN(day), MAX(day) FROM days WHERE WEEKDAY(day) < 5;
1
這是一個簡單的查詢發現平日的2個日期中的數字與MySQL:
set @d1='2013-09-25';
set @d2='2013-10-13';
select floor(datediff(@d2, @d1)/7)*5 +
(case when if(weekday(@d2)>=5,4,weekday(@d2))>=if(weekday(@d1)>=5,4,weekday(@d1))
then if(weekday(@d2)>=5,4,weekday(@d2))-if(weekday(@d1)>=5,4,weekday(@d1))
else 5+if(weekday(@d2)>=5,4,weekday(@d2))-if(weekday(@d1)>=5,4,weekday(@d1)) end) weekdays;
相同的算法,用PHP寫的:
function getWeekDays($d1,$d2){
$d1Array=preg_split('/-/',$d1);
$d2Array=preg_split('/-/',$d2);
$d1w=date('w',mktime(0,0,0,$d1Array[1],$d1Array[2],$d1Array[0]));
$d1w=in_array($d1w,array(0,6))?4:$d1w-1;
$d2w=date('w',mktime(0,0,0,$d2Array[1],$d2Array[2],$d2Array[0]));
$d2w=in_array($d2w,array(0,6))?4:$d2w-1;
$fullWeekDays=floor(((mktime(0,0,0,$d2Array[1],$d2Array[2],$d2Array[0])-mktime(0,0,0,$d1Array[1],$d1Array[2],$d1Array[0]))/86400)/7)*5;
$offset=$d2w>=$d1w?($d2w-$d1w):(5+$d2w-$d1w);
$weekDays=$fullWeekDays+$offset;
return $weekDays;
}
相關問題
- 1. 想要一個SQL來計算兩個日期之間的工作天數
- 2. 計算兩個日期之間的工作日
- 3. 可可 - 計算兩個日期之間的工作日
- 4. 兩個日期之間的計數
- 5. 如何計算兩個日期之間的總工作時間
- 6. 計算兩個日期之間的工作時間
- 7. 兩個日期之間的工作日數[LOGIC]
- 8. 兩個日期之間的工作日數
- 9. 獲取兩個日期之間的工作日時間?
- 10. 如何統計兩個日期之間的週末數
- 11. 如何計算兩個給定日期之間的工作日數
- 12. PLSQL-估計兩個日期之間的特定工作日小時數
- 13. Oracle SQl Dev如何計算兩個日期之間的工作日數
- 14. 日期間的計算在兩個日期之間
- 15. VB.NET - 計算排除日期之間的兩個日期之間的天數
- 16. 在兩個日期之間的某個工作日獲取日期
- 17. 計算日期之間的特定工作日的數量
- 18. SQLite:獲取兩個日期之間的平均工作日
- 19. 兩個日期之間的SQL工作日
- 20. 查找SQL Server中兩個日期之間的工作日
- 21. objective-c中兩個日期之間的工作日?
- 22. 如何統計Postgres中兩個日期之間的星期日除外?
- 23. 計算兩個日期之間的營業日數?
- 24. 如何計算兩個日期之間的營業日數?
- 25. 在Visual Basic中計算兩個日期之間的平日數
- 26. 計算兩個日期之間的日曆月數
- 27. 如何計算日曆兩個日期之間的月份數
- 28. 如何計算兩個日期之間有多少個工作日?
- 29. 兩個日期之間的總工作日與自定義日期
- 30. 過程計算兩個日期之間的日期
請參閱此工作示例:http://sqlfiddle.com/#!2/aa146/2 –
是否可以在給定2個日期的查詢中完全執行此操作?我覺得爲此創造一張桌子有點多。我知道有一個過時的日期會在兩天的日子裏得到不同的結果,我不確定是否有方式說天減去週末什麼的。 – Fallenreaper
當然。在一個查詢中有幾種方法可以做到這一點。您可以將上述內容用作派生表或在視圖中直接使用,而不是創建基表。然後,您可以在查詢中使用派生表或視圖。這很簡單。 –