[MySQL] 查詢5天之內過生日的同事中的跨年問題以及閏年2月29日問題的解決過程

dbasdk發表於2014-09-02
前言:
        遇到朋友提問,如下:
SELECT * FROM ali_users WHERE  DATEDIFF(CAST(CONCAT(DATE_FORMAT(NOW(),'%y'),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),'%y-%m-%d') AS DATE)) <=1
上面的可以找到今天或者明天過生日的
假設今天是12月31日,這時候來年的1月1日的就找不到了,怎麼辦?


1,準備測試資料,需要包含跨年的資料


1.1,準備測試資料的SQL

  1. USE test;
  2. DROP TABLE IF EXISTS ali_users;
  3. CREATE TABLE ali_users (username VARCHAR(10),birthday DATE NOT NULL,iphone VARCHAR(16));
  4. INSERT INTO ali_users SELECT \'MaoYi\',\'1985-09-04\',\'13998786543\' UNION ALL
  5. SELECT \'LiuEr\',\'1985-08-30\',\'13998786543\' UNION ALL
  6. SELECT \'ZhangSan\',\'1981-01-01\',\'13998786543\' UNION ALL
  7. SELECT \'LiSi\',\'1983-01-02\',\'13998786543\' UNION ALL
  8. SELECT \'WangWu\',\'1984-11-01\',\'13998786543\' UNION ALL
  9. SELECT \'ZhaoLiu\',\'1984-11-01\',\'13998786543\' UNION ALL
  10. SELECT \'SongQi\',\'1986-08-31\',\'13998786543\' UNION ALL
  11. SELECT \'HuangBa\',\'1989-09-01\',\'13998786543\' UNION ALL
  12. SELECT \'ZengJiu\',\'1989-09-02\',\'13998786543\' UNION ALL
  13. SELECT \'LuoShi\',\'1985-09-03\',\'13998786543\' UNION ALL
  14. SELECT \'Tom\',\'1995-09-05\',\'13998786543\' UNION ALL
  15. SELECT \'Licy\',\'1991-12-30\',\'13998286543\' UNION ALL
  16. SELECT \'Cari\',\'1992-12-31\',\'13998286543\' UNION ALL
  17. SELECT \'Mark\',\'1992-01-03\',\'13998286543\' UNION ALL
  18. SELECT \'Ruby\',\'1992-01-04\',\'13998286547\';

1.2,在資料庫命令列執行SQL

  1. mysql> USE test;
  2. DATABASE CHANGED
  3. mysql> DROP TABLE IF EXISTS ali_users;
  4. QUERY OK, 0 ROWS affected (0.00 sec)

  5. mysql> CREATE TABLE ali_users (username VARCHAR(10),birthday DATE NOT NULL,iphone VARCHAR(16));
  6. QUERY OK, 0 ROWS affected (0.01 sec)

  7. mysql> INSERT INTO ali_users SELECT \'MaoYi\',\'1985-09-04\',\'13998786543\' UNION ALL
  8.     -> SELECT \'LiuEr\',\'1985-08-30\',\'13998786543\' UNION ALL
  9.     -> SELECT \'ZhangSan\',\'1981-01-01\',\'13998786543\' UNION ALL
  10.     -> SELECT \'LiSi\',\'1983-01-02\',\'13998786543\' UNION ALL
  11.     -> SELECT \'WangWu\',\'1984-11-01\',\'13998786543\' UNION ALL
  12.     -> SELECT \'ZhaoLiu\',\'1984-11-01\',\'13998786543\' UNION ALL
  13.     -> SELECT \'SongQi\',\'1986-08-31\',\'13998786543\' UNION ALL
  14.     -> SELECT \'HuangBa\',\'1989-09-01\',\'13998786543\' UNION ALL
  15.     -> SELECT \'ZengJiu\',\'1989-09-02\',\'13998786543\' UNION ALL
  16.     -> SELECT \'LuoShi\',\'1985-09-03\',\'13998786543\' UNION ALL
  17.     -> SELECT \'Tom\',\'1995-09-05\',\'13998786543\' UNION ALL
  18.     -> SELECT \'Licy\',\'1991-12-30\',\'13998286543\' UNION ALL
  19.     -> SELECT \'Cari\',\'1992-12-31\',\'13998286543\' UNION ALL
  20.     -> SELECT \'Mark\',\'1992-01-03\',\'13998286543\' UNION ALL
  21.     -> SELECT \'Ruby\',\'1992-01-04\',\'13998286547\';
  22. QUERY OK, 15 ROWS affected (0.01 sec)
  23. Records: 15 Duplicates: 0 WARNINGS: 0

  24. mysql> SELECT * FROM ali_users;
  25. +----------+------------+-------------+
  26. | username | birthday | iphone |
  27. +----------+------------+-------------+
  28. | MaoYi | 1985-09-04 | 13998786543 |
  29. | LiuEr | 1985-08-30 | 13998786543 |
  30. | ZhangSan | 1981-01-01 | 13998786543 |
  31. | LiSi | 1983-01-02 | 13998786543 |
  32. | WangWu | 1984-11-01 | 13998786543 |
  33. | ZhaoLiu | 1984-11-01 | 13998786543 |
  34. | SongQi | 1986-08-31 | 13998786543 |
  35. | HuangBa | 1989-09-01 | 13998786543 |
  36. | ZengJiu | 1989-09-02 | 13998786543 |
  37. | LuoShi | 1985-09-03 | 13998786543 |
  38. | Tom | 1995-09-05 | 13998786543 |
  39. | Licy | 1991-12-30 | 13998286543 |
  40. | Cari | 1992-12-31 | 13998286543 |
  41. | Mark | 1992-01-03 | 13998286543 |
  42. | Ruby | 1992-01-04 | 13998286547 |
  43. +----------+------------+-------------+
  44. 15 ROWS IN SET (0.00 sec)

  45. mysql>

2,寫出查詢SQL
SELECT * FROM ali_users WHERE 
/*不跨年的查詢*/
DATEDIFF(CAST(CONCAT(YEAR(NOW()),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),'%y-%m-%d') AS DATE)) BETWEEN 0 AND 4
OR/* or後面的是捎帶解決跨年問題*/ 
DATEDIFF(CAST(CONCAT(YEAR(NOW())+1,DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),'%y-%m-%d') AS DATE)) BETWEEN 0 AND 4;

2,1,跨年問題分析
因為跨年的時候生日欄位通常月份比較小是1月,所以如果利用DATEDIFF來判斷要與月份比較大12月來比較得到相差天數在N天之內的話,就需要YEAR(NOW())+1,當年年份+1再加上月份才能與NOW()比較得出真實的相差天數。

打個比方,今天是2013年12月30日,要查詢出來5天之內的過生日的同事,那麼2014年1月1日2日3日的也在查詢範圍之內,這個時候就需要CONCAT(YEAR(NOW())+1,DATE_FORMAT(birthday,'-%m-%d')年份加1形成2014-01-01才能與今天的2013-12-30進行比較得出正確的相差天數。

2.2,5天之內的設定
        N天之內,用 BETWEEN 0 AND N 來判斷,如果是5天之內(包含今天)那麼N值就是4,就是 BETWEEN 0 AND 4


3,驗證資料
比如提醒最近5天之內(包括今日)過生日的同事,生日快樂。

3.1,查詢的資料都在今年之內的,比如今天是8月30日,那麼需要執行的SQL如下:
SELECT * FROM ali_users WHERE 
DATEDIFF(CAST(CONCAT(YEAR(NOW()),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),'%y-%m-%d') AS DATE)) BETWEEN 0 AND 4
OR/* or後面的是捎帶解決跨年問題*/ 
DATEDIFF(CAST(CONCAT(YEAR(NOW())+1,DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),'%y-%m-%d') AS DATE)) BETWEEN 0 AND 4;

查詢的結果應該是從今天8月30日到9月3日之間過生日的同事,包括LiuEr,SongQi,HuangBa,ZengJiu,LuoShi;
執行如下所示,符合預期,如下所示:

  1. mysql> SELECT * FROM ali_users WHERE
  2.     -> DATEDIFF(CAST(CONCAT(YEAR(NOW()),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(NOW(),\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3.     -> OR/* or後面的是捎帶解決跨年問題*/
  4.     -> DATEDIFF(CAST(CONCAT(YEAR(NOW())+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(NOW(),\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5.     -> ;
  6. +----------+------------+-------------+
  7. | username | birthday | iphone |
  8. +----------+------------+-------------+
  9. | LiuEr | 1985-08-30 | 13998786543 |
  10. | SongQi | 1986-08-31 | 13998786543 |
  11. | HuangBa | 1989-09-01 | 13998786543 |
  12. | ZengJiu | 1989-09-02 | 13998786543 |
  13. | LuoShi | 1985-09-03 | 13998786543 |
  14. +----------+------------+-------------+
  15. 5 ROWS IN SET (0.00 sec)

  16. mysql>

3.2,查詢的生日有跨年的
        比如今天是2013年12月30日,要查詢5天之內過生日的同事,那麼就有2013年的12月30日31日過生日的,也有2014年1月1日2日3日過生日的同事,
因為今天是8月30日,所以要把Step#2中的SQL的NOW()改成'2013-12-30 00:10:10'來進行測試,SQL整理如下:
SELECT * FROM ali_users WHERE 
DATEDIFF(CAST(CONCAT(YEAR('2013-12-30 00:10:10'),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT('2013-12-30 00:10:10','%y-%m-%d') AS DATE)) BETWEEN 0 AND 4
OR/* or後面的是捎帶解決跨年問題*/ 
DATEDIFF(CAST(CONCAT(YEAR('2013-12-30 00:10:10')+1,DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT('2013-12-30 00:10:10','%y-%m-%d') AS DATE)) BETWEEN 0 AND 4;

查詢的結果應該是從今天8月30日到9月3日之間過生日的同事,包括Licy,Cari,ZhangSan,LiSi,Mark。
執行結果如下所示,符合預期,如下所示:

  1. mysql> SELECT * FROM ali_users WHERE
  2.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2013-12-30 00:10:10\'),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2013-12-30 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3.     -> OR/* or後面的是捎帶解決跨年問題*/
  4.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2013-12-30 00:10:10\')+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2013-12-30 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5.     -> ;
  6. +----------+------------+-------------+
  7. | username | birthday | iphone |
  8. +----------+------------+-------------+
  9. | ZhangSan | 1981-01-01 | 13998786543 |
  10. | LiSi | 1983-01-02 | 13998786543 |
  11. | Licy | 1991-12-30 | 13998286543 |
  12. | Cari | 1992-12-31 | 13998286543 |
  13. | Mark | 1992-01-03 | 13998286543 |
  14. +----------+------------+-------------+
  15. 5 ROWS IN SET (0.00 sec)

  16. mysql>

5,補充閏年判斷
有朋友提醒,閏年2月29日生日的話,可能查詢不到,想到確實沒有考慮到這個特殊的日期。
5.1,準備測試資料SQL,包含1980-02-29這一天生日的朋友。
INSERT INTO ali_users  SELECT 'Jeff','1980-02-29','13998786549' 
UNION ALL SELECT 'WeiYa','1980-02-28','13998786549' 
UNION ALL SELECT 'XiaoTeng','1980-03-01','13998786549' 
UNION ALL SELECT 'HeSheng','1980-03-02','13998786549'
UNION ALL SELECT 'JingPan','1980-03-03','13998786549'
UNION ALL SELECT 'WuHong','1986-03-04','13998786549';

5.2,錄入測試資料
  1. mysql> use test;
  2. Reading table information for completion of table and column names
  3. You can turn off this feature to get a quicker startup with -A

  4. Database changed
  5. mysql> INSERT INTO ali_users SELECT \'Jeff\',\'1980-02-29\',\'13998786549\' UNION ALL SELECT \'XiaoTeng\',\'1980-03-01\',\'13998786549\'
  6.     -> UNION ALL SELECT \'HeSheng\',\'1980-03-02\',\'13998786549\'
  7.     -> UNION ALL SELECT \'JingPan\',\'1980-03-03\',\'13998786549\'
  8.     -> UNION ALL SELECT \'WuHong\',\'1986-03-04\',\'13998786549\';
  9. Query OK, 5 rows affected (0.00 sec)
  10. Records: 5 Duplicates: 0 Warnings: 0

  11. mysql>

5.3,執行原來的舊版本的SQL查詢檢查結果

把Step#2中的SQL的NOW()改成'2014-02-28 00:10:10'來進行測試,確實沒有包括2月29日生日的同事,如下所示:
  1. mysql> SELECT * FROM ali_users WHERE
  2.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2014-02-28 00:10:10\'),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2014-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3.     -> OR/* or後面的是捎帶解決跨年問題*/
  4.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2014-02-28 00:10:10\')+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2014-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5.     -> ;
  6. +----------+------------+-------------+
  7. | username | birthday | iphone |
  8. +----------+------------+-------------+
  9. | XiaoTeng | 1980-03-01 | 13998786549 |
  10. | HeSheng | 1980-03-02 | 13998786549 |
  11. | JingPan | 1980-03-03 | 13998786549 |
  12. | WuHong | 1986-03-04 | 13998786549 |
  13. +----------+------------+-------------+
  14. 4 rows in set, 2 warnings (0.00 sec)


  15. mysql>

5.4,先建立一個儲存函式f_isleap_year判斷當年年份是否是閏年
5.4.1 準備判斷是否閏年的SQL
  1. DELIMITER $$
  2. USE `test`$$
  3. DROP FUNCTION IF EXISTS `f_not_leap_year`$$
  4. CREATE FUNCTION `f_not_leap_year`(p_year BIGINT) RETURNS BOOLEAN
  5. BEGIN
  6. /*是閏年則返回0(false),不是閏年則返回1(true)*/
  7. DECLARE v_flag INT DEFAULT 0;
  8. /*①、普通年能被4整除且不能被100整除的為閏年。(如2004年就是閏年,1901年不是閏年)*/
  9. IF (p_year%4)=0 AND (p_year%100)>0 THEN
  10. SET v_flag=0;
  11. /*②、世紀年能被400整除的是閏年。(如2000年是閏年,1900年不是閏年) */
  12. ELSEIF (p_year%400)=0 THEN
  13. SET v_flag=0;
  14. /*③、對於數值很大的年份,這年如果能整除3200,並且能整除172800則是閏年。如172800年是閏年,
  15. 86400年不是閏年(因為雖然能整除3200,但不能整除172800)(此按一回歸年365天5h48\'45.5\'\'計算)。
  16. */
  17. ELSEIF (p_year%3200)=0 AND (p_year%172800)=0 THEN
  18. SET v_flag=0;
  19. ELSE
  20. SET v_flag=1;
  21. END IF;
  22. RETURN v_flag;
  23.  END$$
  24. DELIMITER ;

儲存函式執行如下圖所示:
 


5.4.2 準備SQL語句

  1. SELECT * FROM ali_users WHERE
  2. DATEDIFF(CAST(CONCAT(YEAR(NOW()),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(NOW(),\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3. OR/* or後面的是捎帶解決跨年問題*/
  4. DATEDIFF(CAST(CONCAT(YEAR(NOW())+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(NOW(),\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5. OR /*補充閏年2月29日的生日問題*/
  6. (
  7. f_not_leap_year(YEAR(NOW()))
  8. AND DATE_FORMAT(birthday,\'-%m-%d\')=\'-02-29\'
  9. AND DATEDIFF(CAST(CONCAT(\'2000\',DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(CONCAT(\'2000\',DATE_FORMAT(NOW(),\'-%m-%d\')) AS DATE))
  10. BETWEEN 0 AND 4
  11. );

5.4.3 在非閏年的時候,驗證閏年2月29日生日,選擇2014年非閏年測試
把Step#2中的SQL的NOW()改成'2014-02-28 00:10:10'來進行測試,SQL如下所示:
SELECT * FROM ali_users WHERE 
DATEDIFF(CAST(CONCAT(YEAR('2014-02-28 00:10:10'),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT('2014-02-28 00:10:10','%y-%m-%d') AS DATE)) BETWEEN 0 AND 4
OR/* or後面的是捎帶解決跨年問題*/ 
DATEDIFF(CAST(CONCAT(YEAR('2014-02-28 00:10:10')+1,DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT('2014-02-28 00:10:10','%y-%m-%d') AS DATE)) BETWEEN 0 AND 4
OR /*補充閏年2月29日的生日方法*/
(
f_not_leap_year(YEAR('2014-02-28 00:10:10')) 
AND DATE_FORMAT(birthday,'-%m-%d')='-02-29' 
AND DATEDIFF(CAST(CONCAT('2000',DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(CONCAT('2000',DATE_FORMAT('2000-02-28 00:10:10','-%m-%d')) AS DATE)) 
BETWEEN 0 AND 4
);

執行SQL檢驗成果,如果當天是2014-02-28,看到已經有2月29日的生日的同事被記錄進來了,其實包含了2月28日、2月29日、3月1日、3月2日、3月3日、3月4日的生日的同事。
PS:因為2月29日在當年不存在,所以不算這5天之內的範疇,執行結果如下所示:
  1. mysql> SELECT * FROM ali_users WHERE
  2.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2014-02-28 00:10:10\'),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2014-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3.     -> OR/* or後面的是捎帶解決跨年問題*/
  4.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2014-02-28 00:10:10\')+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2014-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5.     -> OR /*補充閏年2月29日的生日方法*/
  6.     -> (
  7.     -> f_not_leap_year(YEAR(\'2014-02-28 00:10:10\'))
  8.     -> AND DATE_FORMAT(birthday,\'-%m-%d\')=\'-02-29\'
  9.     -> AND DATEDIFF(CAST(CONCAT(\'2000\',DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(CONCAT(\'2000\',DATE_FORMAT(\'2000-02-28 00:10:10\',\'-%m-%d\')) AS DATE))
  10.     -> BETWEEN 0 AND 4
  11.     -> );
  12. +----------+------------+-------------+
  13. | username | birthday | iphone |
  14. +----------+------------+-------------+
  15. | Jeff | 1980-02-29 | 13998786549 |
  16. | XiaoTeng | 1980-03-01 | 13998786549 |
  17. | HeSheng | 1980-03-02 | 13998786549 |
  18. | JingPan | 1980-03-03 | 13998786549 |
  19. | WuHong | 1986-03-04 | 13998786549 |
  20. | WeiYa | 1980-02-28 | 13998786549 |
  21. +----------+------------+-------------+
  22. 6 rows in set, 2 warnings (0.00 sec)


  23. mysql>

5.4.4 在閏年的時候,驗證閏年2月29日生日,選擇2004年閏年測試
把Step#2中的SQL的NOW()改成'2004-02-28 00:10:10'來進行測試,SQL如下所示:
  1. SELECT * FROM ali_users WHERE
  2. DATEDIFF(CAST(CONCAT(YEAR(\'2004-02-28 00:10:10\'),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2004-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3. OR/* or後面的是捎帶解決跨年問題*/
  4. DATEDIFF(CAST(CONCAT(YEAR(\'2004-02-28 00:10:10\')+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2004-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5. OR /*補充閏年2月29日的生日方法*/
  6. (
  7. f_not_leap_year(YEAR(\'2004-02-28 00:10:10\'))
  8. AND DATE_FORMAT(birthday,\'-%m-%d\')=\'-02-29\'
  9. AND DATEDIFF(CAST(CONCAT(\'2000\',DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(CONCAT(\'2000\',DATE_FORMAT(\'2004-02-28 00:10:10\',\'-%m-%d\')) AS DATE))
  10. BETWEEN 0 AND 4
  11. );

執行SQL檢驗成果,如果當天是2004-02-28,看到已經有2月29日的生日的同事被記錄進來了,其實包含了2月28日、2月29日、3月1日、3月2日、3月3日的生日的同事。
PS:因為2月29日在當年存在,所以算這5天之內的範疇,執行結果如下所示:

  1. mysql> SELECT * FROM ali_users WHERE
  2.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2004-02-28 00:10:10\'),DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2004-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  3.     -> OR/* or後面的是捎帶解決跨年問題*/
  4.     -> DATEDIFF(CAST(CONCAT(YEAR(\'2004-02-28 00:10:10\')+1,DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(DATE_FORMAT(\'2004-02-28 00:10:10\',\'%y-%m-%d\') AS DATE)) BETWEEN 0 AND 4
  5.     -> OR /*補充閏年2月29日的生日方法*/
  6.     -> (
  7.     -> f_not_leap_year(YEAR(\'2004-02-28 00:10:10\'))
  8.     -> AND DATE_FORMAT(birthday,\'-%m-%d\')=\'-02-29\'
  9.     -> AND DATEDIFF(CAST(CONCAT(\'2000\',DATE_FORMAT(birthday,\'-%m-%d\'))AS DATE),CAST(CONCAT(\'2000\',DATE_FORMAT(\'2004-02-28 00:10:10\',\'-%m-%d\')) AS DATE))
  10.     -> BETWEEN 0 AND 4
  11.     -> );
  12. +----------+------------+-------------+
  13. | username | birthday | iphone |
  14. +----------+------------+-------------+
  15. | Jeff | 1980-02-29 | 13998786549 |
  16. | XiaoTeng | 1980-03-01 | 13998786549 |
  17. | HeSheng | 1980-03-02 | 13998786549 |
  18. | JingPan | 1980-03-03 | 13998786549 |
  19. | WeiYa | 1980-02-28 | 13998786549 |
  20. +----------+------------+-------------+
  21. 5 rows in set (0.00 sec)


  22. mysql>

6,總結
    這種類似的場景,涉及到查詢多少天之內的資料,以及涉及到跨度範圍比較大的查詢(比如跨年或者跨部門,跨省、跨國、跨公司等等),都可以用這個思路來需求解決問題的方法。
    
    

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/29734436/viewspace-1262018/,如需轉載,請註明出處,否則將追究法律責任。

相關文章