最新文章专题视频专题问答1问答10问答100问答1000问答2000关键字专题1关键字专题50关键字专题500关键字专题1500TAG最新视频文章推荐1 推荐3 推荐5 推荐7 推荐9 推荐11 推荐13 推荐15 推荐17 推荐19 推荐21 推荐23 推荐25 推荐27 推荐29 推荐31 推荐33 推荐35 推荐37视频文章20视频文章30视频文章40视频文章50视频文章60 视频文章70视频文章80视频文章90视频文章100视频文章120视频文章140 视频2关键字专题关键字专题tag2tag3文章专题文章专题2文章索引1文章索引2文章索引3文章索引4文章索引5123456789101112131415文章专题3
问答文章1 问答文章501 问答文章1001 问答文章1501 问答文章2001 问答文章2501 问答文章3001 问答文章3501 问答文章4001 问答文章4501 问答文章5001 问答文章5501 问答文章6001 问答文章6501 问答文章7001 问答文章7501 问答文章8001 问答文章8501 问答文章9001 问答文章9501
当前位置: 首页 - 科技 - 知识百科 - 正文

[MySQL]--)查询5天之内过生日的同事中的跨年问题的解决过程_MySQL

来源:懂视网 责编:小采 时间:2020-11-09 19:43:44
文档

[MySQL]--)查询5天之内过生日的同事中的跨年问题的解决过程_MySQL

[MySQL]--)查询5天之内过生日的同事中的跨年问题的解决过程_MySQL:前言: 遇到朋友提问,如下: SELECT * FROM ali_users WHERE DATEDIFF(CAST(CONCAT(DATE_FORMAT(NOW(),'%y'),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),&#
推荐度:
导读[MySQL]--)查询5天之内过生日的同事中的跨年问题的解决过程_MySQL:前言: 遇到朋友提问,如下: SELECT * FROM ali_users WHERE DATEDIFF(CAST(CONCAT(DATE_FORMAT(NOW(),'%y'),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),&#

前言:

遇到朋友提问,如下:

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

1,准备测试数据,需要包含跨年的数据

1.1,准备测试数据的SQL

USE test;
DROP TABLE IF EXISTS ali_users;
CREATE TABLE ali_users (username VARCHAR(10),birthday DATE NOT NULL,iphone VARCHAR(16));
INSERT INTO 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\';
1.2,在数据库命令行执行SQL

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

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

mysql> INSERT INTO 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 IN SET (0.00 sec)

mysql>
2,写出查询SQL
SELECT * FROM ali_users WHERE
2,1,跨年问题分析
因为跨年的时候生日字段通常月份比较小是1月,所以如果利用DATEDIFF来判断要与月份比较大12月来比较得到相差天数在N天之内的话,就需要YEAR(NOW())+1,当年年份+1再加上月份才能与NOW()比较得出真实的相差天数。
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
查询的结果应该是从今天8月30日到9月3日之间过生日的同事,包括LiuEr,SongQi,HuangBa,ZengJiu,LuoShi;

mysql> 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
-> ;
+----------+------------+-------------+
| 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 IN SET (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整理如下:
mysql> 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
-> ;
+----------+------------+-------------+
| username | birthday | iphone |
+----------+------------+-------------+
| ZhangSan | 1981-01-01 | 13998786543 |
| LiSi | 1983-01-02 | 13998786543 |
| Licy | 1991-12-30 | 13998286543 |
| Cari | 1992-12-31 | 13998286543 |
| Mark | 1992-01-03 | 13998286543 |
+----------+------------+-------------+
5 ROWS IN SET (0.00 sec)

mysql>
4,总结

声明:本网页内容旨在传播知识,若有侵权等问题请及时与本网联系,我们将在第一时间删除处理。TEL:177 7030 7066 E-MAIL:11247931@qq.com

文档

[MySQL]--)查询5天之内过生日的同事中的跨年问题的解决过程_MySQL

[MySQL]--)查询5天之内过生日的同事中的跨年问题的解决过程_MySQL:前言: 遇到朋友提问,如下: SELECT * FROM ali_users WHERE DATEDIFF(CAST(CONCAT(DATE_FORMAT(NOW(),'%y'),DATE_FORMAT(birthday,'-%m-%d'))AS DATE),CAST(DATE_FORMAT(NOW(),&#
推荐度:
标签: 过程 生日 同事
  • 热门焦点

最新推荐

猜你喜欢

热门推荐

专题
Top