2012-10-17 82 views

回答

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; 
+0

請參閱此工作示例:http://sqlfiddle.com/#!2/aa146/2 –

+0

是否可以在給定2個日期的查詢中完全執行此操作?我覺得爲此創造一張桌子有點多。我知道有一個過時的日期會在兩天的日子裏得到不同的結果,我不確定是否有方式說天減去週末什麼的。 – Fallenreaper

+0

當然。在一個查詢中有幾種方法可以做到這一點。您可以將上述內容用作派生表或在視圖中直接使用,而不是創建基表。然後,您可以在查詢中使用派生表或視圖。這很簡單。 –

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; 
} 
相關問題