Mysql>
2. write the query SQL
SELECT * FROM ali_users WHERE
2, 1, cross-year problem analysis
Because the birthday field in the cross-year period is usually less than the month of January, if DATEDIFF is used to determine that the difference between the date and month is greater than that in December, the number of days is less than N days, you need YEAR (NOW () + 1, YEAR + 1 of the current YEAR plus month to compare with NOW () to get the actual days of difference.
2.2, set within 5 days
In N days, BETWEEN 0 and n is used to determine. if it is within 5 days (including today), then the value of N is 4, that is, BETWEEN 0 AND 4.
3. verify data
For example, to remind colleagues who have their birthdays in the last five days (including today), happy birthday.
3.1. the queried data is within this year. for example, if today is July 22, August 30, the SQL statement to be executed is as follows:
SELECT * FROM ali_users WHERE
The query results should be from the end of February 3 to February 3, August 30-9, including LiuEr, SongQi, HuangBa, ZengJiu, and 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 is followed by the renewal belt to solve the cross-year problem */
-> 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-0903 | 13998786543 |
+ ---------- + ------------ + ------------- +
5 rows in set (0.00 sec)
Mysql>
3.2, the queried birthday has a new year
For example, if we want to query my colleagues who have their birthdays within five days, we will have their birthdays on October 31, and those who have birthdays on October 2 and 3, because today is July 22, 2013, we need to change the SQL now () in Step #2 to '2017-12-30 00:10:10 'for testing. the SQL is organized as follows:
Mysql> SELECT * FROM ali_users WHERE
-> DATEDIFF (CAST (CONCAT (YEAR (\ '2017-12-30 00:10:10 \ '), DATE_FORMAT (birthday, \'-% m-% d \')) as date), CAST (DATE_FORMAT (\ '2017-12-30 00:10:10 \ ', \' % y-% m-% d \ ') as date) BETWEEN 0 AND 4
-> OR/* or is followed by the renewal belt to solve the cross-year problem */
-> DATEDIFF (CAST (CONCAT (YEAR (\ '2017-12-30 00:10:10 \ ') + 1, DATE_FORMAT (birthday, \'-% m-% d \')) as date), CAST (DATE_FORMAT (\ '2017-12-30 00:10:10 \ ', \' % y-% m-% d \ ') as date) BETWEEN 0 AND 4
->;
+ ---------- + ------------ + ------------- +
| Username | birthday | iphone |
+ ---------- + ------------ + ------------- +
| ZhangSan | 1981-01-01 | 13998786543 |
| LiSi | 1981-01-02 | 13998786543 |
| Licy | 1991-12-30 | 13998286543 |
| Cari | 1992-12-31 | 13998286543 |
| Mark | 13998286543 |
+ ---------- + ------------ + ------------- +
5 rows in set (0.00 sec)
The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion;
products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the
content of the page makes you feel confusing, please write us an email, we will handle the problem
within 5 days after receiving your email.
If you find any instances of plagiarism from the community, please send an email to:
info-contact@alibabacloud.com
and provide relevant evidence. A staff member will contact you within 5 working days.