前言: 遇到朋友提问,如下: 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日的就找不到了,怎么办?
mysql>INSERTINTO ali_users SELECT \‘MaoYi\‘,\‘1985-09-04\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘LiuEr\‘,\‘1985-08-30\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘ZhangSan\‘,\‘1981-01-01\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘LiSi\‘,\‘1983-01-02\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘WangWu\‘,\‘1984-11-01\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘ZhaoLiu\‘,\‘1984-11-01\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘SongQi\‘,\‘1986-08-31\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘HuangBa\‘,\‘1989-09-01\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘ZengJiu\‘,\‘1989-09-02\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘LuoShi\‘,\‘1985-09-03\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘Tom\‘,\‘1995-09-05\‘,\‘13998786543\‘ UNION ALL
->SELECT \‘Licy\‘,\‘1991-12-30\‘,\‘13998286543\‘ UNION ALL
->SELECT \‘Cari\‘,\‘1992-12-31\‘,\‘13998286543\‘ UNION ALL
->SELECT \‘Mark\‘,\‘1992-01-03\‘,\‘13998286543\‘ UNION ALL
->SELECT \‘Ruby\‘,\‘1992-01-04\‘,\‘13998286547\‘;
QUERY OK, 15 ROWS affected (0.01 sec)
Records: 15 Duplicates: 0 WARNINGS: 0
mysql>SELECT*FROM ali_users;
+----------+------------+-------------+
| username | birthday | iphone |
+----------+------------+-------------+
| MaoYi | 1985-09-04 | 13998786543 |
| LiuEr | 1985-08-30 | 13998786543 |
| ZhangSan | 1981-01-01 | 13998786543 |
| LiSi | 1983-01-02 | 13998786543 |
| WangWu | 1984-11-01 | 13998786543 |
| ZhaoLiu | 1984-11-01 | 13998786543 |
| SongQi | 1986-08-31 | 13998786543 |
| HuangBa | 1989-09-01 | 13998786543 |
| ZengJiu | 1989-09-02 | 13998786543 |
| LuoShi | 1985-09-03 | 13998786543 |
| Tom | 1995-09-05 | 13998786543 |
| Licy | 1991-12-30 | 13998286543 |
| Cari | 1992-12-31 | 13998286543 |
| Mark | 1992-01-03 | 13998286543 |
| Ruby | 1992-01-04 | 13998286547 |
+----------+------------+-------------+
15 ROWS INSET(0.00 sec)
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.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;
->DATEDIFF(CAST(CONCAT(YEAR(NOW()),DATE_FORMAT(birthday,\‘-%m-%d\‘))ASDATE),CAST(DATE_FORMAT(NOW(),\‘%y-%m-%d\‘)ASDATE))BETWEEN 0 AND 4
->OR/* or后面的是捎带解决跨年问题*/
->DATEDIFF(CAST(CONCAT(YEAR(NOW())+1,DATE_FORMAT(birthday,\‘-%m-%d\‘))ASDATE),CAST(DATE_FORMAT(NOW(),\‘%y-%m-%d\‘)ASDATE))BETWEEN 0 AND 4
->;
+----------+------------+-------------+
| username | birthday | iphone |
+----------+------------+-------------+
| LiuEr | 1985-08-30 | 13998786543 |
| SongQi | 1986-08-31 | 13998786543 |
| HuangBa | 1989-09-01 | 13998786543 |
| ZengJiu | 1989-09-02 | 13998786543 |
| LuoShi | 1985-09-03 | 13998786543 |
+----------+------------+-------------+
5 ROWS INSET(0.00 sec)
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;