[MySQL]--)查询5天之内过生日的同事中的闰年2月29日问题的解决过程_MySQL
上次写了查询5天之内过生日的同事中的跨年问题的解决过程,网址为:http://blog.csdn.net/mchdba/article/details/38952033 ,其中漏了一个闰年2月29日生日的细节问题,现在补充一下这个问题的处理过程:
5,补充闰年判断
有朋友提醒,闰年2月29日生日的话,可能查询不到,想到确实没有考虑到这个特殊的日期。
5.1,准备测试数据SQL,包含1980-02-29这一天生日的朋友。
INSERT INTO ali_users SELECT 'Jeff','1980-02-29','13998786549'
5.2,录入测试数据
mysql> use test;
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A
Database changed
mysql> INSERT INTO ali_users SELECT \'Jeff\',\'1980-02-29\',\'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\';
Query OK, 5 rows affected (0.00 sec)
Records: 5 Duplicates: 0 Warnings: 0
mysql>
5.3,执行原来的旧版本的SQL查询检查结果
mysql> 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
-> ;
+----------+------------+-------------+
| username | birthday | iphone |
+----------+------------+-------------+
| XiaoTeng | 1980-03-01 | 13998786549 |
| HeSheng | 1980-03-02 | 13998786549 |
| JingPan | 1980-03-03 | 13998786549 |
| WuHong | 1986-03-04 | 13998786549 |
+----------+------------+-------------+
4 rows in set, 2 warnings (0.00 sec)
mysql>
5.4,先建立一个存储函数f_isleap_year判断当年年份是否是闰年
DELIMITER $$
USE `test`$$
DROP FUNCTION IF EXISTS `f_not_leap_year`$$
CREATE FUNCTION `f_not_leap_year`(p_year BIGINT) RETURNS BOOLEAN
BEGIN
/*是闰年则返回0(false),不是闰年则返回1(true)*/
DECLARE v_flag INT DEFAULT 0;
/*①、普通年能被4整除且不能被100整除的为闰年。(如2004年就是闰年,1901年不是闰年)*/
IF (p_year%4)=0 AND (p_year%100)>0 THEN
SET v_flag=0;
/*②、世纪年能被400整除的是闰年。(如2000年是闰年,1900年不是闰年) */
ELSEIF (p_year%400)=0 THEN
SET v_flag=0;
/*③、对于数值很大的年份,这年如果能整除3200,并且能整除172800则是闰年。如172800年是闰年,
86400年不是闰年(因为虽然能整除3200,但不能整除172800)(此按一回归年365天5h48\'45.5\'\'计算)。
*/
ELSEIF (p_year%3200)=0 AND (p_year%172800)=0 THEN
SET v_flag=0;
ELSE
SET v_flag=1;
END IF;
RETURN v_flag;
END$$
DELIMITER ;
存储函数执行如下图所示:

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
OR /*补充闰年2月29日的生日问题*/
(
f_not_leap_year(YEAR(NOW()))
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(NOW(),\'-%m-%d\')) AS DATE))
BETWEEN 0 AND 4
);
5.4.3 在非闰年的时候,验证闰年2月29日生日,选择2014年非闰年测试
SELECT * FROM ali_users WHERE
执行SQL检验成果,如果当天是2014-02-28,看到已经有2月29日的生日的同事被记录进来了,其实包含了2月28日、2月29日、3月1日、3月2日、3月3日、3月4日的生日的同事。
PS:因为2月29日在当年不存在,所以不算这5天之内的范畴,执行结果如下所示:
mysql> 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
-> );
+----------+------------+-------------+
| username | birthday | iphone |
+----------+------------+-------------+
| Jeff | 1980-02-29 | 13998786549 |
| XiaoTeng | 1980-03-01 | 13998786549 |
| HeSheng | 1980-03-02 | 13998786549 |
| JingPan | 1980-03-03 | 13998786549 |
| WuHong | 1986-03-04 | 13998786549 |
| WeiYa | 1980-02-28 | 13998786549 |
+----------+------------+-------------+
6 rows in set, 2 warnings (0.00 sec)
mysql>
5.4.4 在闰年的时候,验证闰年2月29日生日,选择2004年闰年测试
把Step#2中的SQL的NOW()改成'2004-02-28 00:10:10'来进行测试,SQL如下所示:
SELECT * FROM ali_users WHERE
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
OR/* or后面的是捎带解决跨年问题*/
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
OR /*补充闰年2月29日的生日方法*/
(
f_not_leap_year(YEAR(\'2004-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(\'2004-02-28 00:10:10\',\'-%m-%d\')) ASDATE))
BETWEEN 0 AND 4
);
mysql> SELECT * FROM ali_users WHERE
-> 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
-> OR/* or后面的是捎带解决跨年问题*/
-> 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
-> OR /*补充闰年2月29日的生日方法*/
-> (
-> f_not_leap_year(YEAR(\'2004-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(\'2004-02-28 00:10:10\',\'-%m-%d\')) AS DATE))
-> BETWEEN 0 AND 4
-> );
+----------+------------+-------------+
| username | birthday | iphone |
+----------+------------+-------------+
| Jeff | 1980-02-29 | 13998786549 |
| XiaoTeng | 1980-03-01 | 13998786549 |
| HeSheng | 1980-03-02 | 13998786549 |
| JingPan | 1980-03-03 | 13998786549 |
| WeiYa | 1980-02-28 | 13998786549 |
+----------+------------+-------------+
5 rows in set (0.00 sec)
mysql>

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics



A leap year refers to a leap year that occurs every four years in the Gregorian calendar, that is, a year of 366 days is inserted between ordinary 365-day years. Its purpose is to conform to the solar year. According to the provisions of the Gregorian calendar, there are generally two situations in leap years: ① the year is evenly divisible by 4, but not 100; ② the year is evenly divisible by 400. In this article, we will introduce to you the method of realizing leap year judgment based on PHP programming language.

A leap year has 366 days, while an ordinary year has 365 days. The task is to check whether a given year is a leap year through a program. The logic of the judgment can be implemented by checking whether the year is divisible by 400 or 4, but if it is not divisible by these two numbers, it is an ordinary year. ExampleInput-:year=2000Output-:2000isaLeapYearInput-:year=101Output-:101isnotaLeapyear algorithmStartStep1->declarefunctionbooltocheckifyearifaleapyearornotboolcheck(intye

In Back to the Future 1999, players will face many level challenges, and each level brings completely different challenges. As one of the levels, 6-24 will definitely have many players thinking about it. You know how to challenge this level, so the following will also bring relevant clearance methods. Back to the Future 19996-24 Clearance Method 1. After burning on level 30 in one sentence, wait for the boss to be stunned and give him a heavy beating. 2. Prioritize using main C and 142d to burn in one round. 3. In the second round, use the auxiliary and nanny's small skills to burn and the main C card to build a big move. 4. The boss will be stunned in three rounds, and then he will be beaten directly with his ultimate move and damage skills.

Share how to pass the level of Ming Tide Fantasyland Difficulty 3. Many people in Mingchao are completing level 3 of this fantasyland. This level is actually a bit difficult. Many friends don’t know what to do yet, but don’t worry, the editor brings it to you. If you have read the strategies in it, come and give it a try. How to pass the Bell Turtle character in Difficulty Level 3? Strengthening resonance skills can improve the effect of our aerodynamics. In this way, the attack efficiency will be increased and the damage will be greater. However, if you have Effect replacement can also be exchanged. [Metaphor] Choice for the first three levels: Sun in the Forest: It can greatly increase the damage of Jiyan. If the skill is used more often, it can produce a very good critical hit effect. Protective device: Use resonance technology

Selection sort is an aggressive algorithm used to find the smallest number from an array and place it in the first position. The next array to be traversed will start at the index, close to where the smallest number is placed. The process of selection sort selects the first smallest element in the list of elements and places it in the first position. Repeat the same operation for the remaining elements in the list until all elements are sorted. Consider the following list - first pass Sm=a[0]=30Sma[1]

Many friends want to upgrade their computer systems to win10 system, but they don’t know how long the upgrade process will take. Today I have brought you a detailed introduction to the time required to update to win10. Come and take a look. How long does it take to update to win10: 1. It varies from about 40 minutes to 2/3 hours depending on the performance of each user's machine. 2. The time to configure performance is also different, which is closely related to the amount of data in the old system. 3. If the computer has a lot of stuff and a mechanical keyboard, it will be even slower. 4. If you upgrade from win7/8 to win10, it will take about an hour and a half. 5. Reserve enough time in advance before upgrading to back up important things to avoid loss.

A small adventure is an early branch in Dragon's Dogma, so how can this task be completed? Players first need to go to Mevi, accept this mission at the grocery store here, and buy pills to complete this branch. Generally speaking, it is very simple. The detailed content can be found in this small adventure mission graphic guide. Let’s learn more and take a look together. How to survive a small adventure in Dragon's Dogma 2: 1. First, take the ox cart north of Velenworth to Meve. 2. After arriving in Mevi, go to Lunnai’s props to take you, and talk to the NPC here to start the mission. 3. Then the player enters the store behind the door, talks to the uncle here, and buys pills. 4. After purchasing, return to the door and provide the pills to the NPC. 5. Subsequently

Detailed explanation of Golang compilation process Golang (also known as Go) is a programming language developed by Google. It has the characteristics of simplicity, efficiency, concurrency, etc., so it has received widespread attention and application. When programming with Golang, compilation is a very important link. This article will introduce the Golang compilation process in detail and provide specific code examples. 1. Compilation process of Golang source code Lexical Analysis (LexicalAnalysis) The first step in the compilation process is the word
