How to compare dates and display records within 5 days

Source: Internet
Author: User
Tags mysql functions
How to compare dates and display records within 5 days. I posted a post a few days ago about displaying the comparison of these dates within five days, www.oso.com. cnforumread. php? Theme_id7459 focuses on SELECTsomethingFROMtab I posted a post a few days ago about displaying the date comparison within five days,

Http://www.oso.com.cn/forum/read.php? Theme_id = 7459.

The main argument is to focus on SELECT something FROM table
WHERE TO_DAYS (NOW ()-TO_DAYS (date_col) <= 5; I tried it for a long time and the result was still incorrect. today I read the MYSQL function and finally got the result, I don't dare to share it with others. I will post it for your research. (although the technical content is not high, don't drop my persimmon)

The TO_DAYS (DATE) function of MYSQL is described as follows:
Returns the total number of days from DATE to 0 A.M. I tested it.
Mysql> select to_days (now (0 ));
+ -------------------------- +
| TO_DAYS (NOW () |
+ -------------------------- +
| 1, 730839 |
+ -------------------------- +

The current time is the total number of days from 0 a.m.. then I try to use the above statement to test;

Mysql> select TO_DAYS (NOW ()-TO_DAYS (date_col) <= 5;
Result:
ERROR 1054: Unknown column date_col in field first

This cannot be done, so I try to directly add the 5 generation to date_col.

Mysql> select to_days (now ()-to_days (5 );
Result:
+ --------------------------- +
| To_days (now ()-to_days (5) |
+ --------------------------- +
| NULL |
+ --------------------------- +

Ah? No, right? That's not the case?
I will try again
Mysql> select ....

Suddenly, I think that to_days (now () is an integer, so I can directly perform an integer operation. why to_days (date? Test now

Mysql> select to_days (now ()-5;
+ -------------------------- +
| To_days (now ()-5 |
+ -------------------------- +
| 1, 730834 |
+ -------------------------- +


OK, long live. I finally got the desired result.

The habit of storing databases is that DATEANDTIME can be directly assigned values with NOW (), and displayed without formatting,

Below is a part of my library structure
Create table infomess (
Infoid int (11) not null auto_increment,
Topic varchar (255) not null,
......
Email varchar (50 ),
Dateandtime datetime DEFAULT 0000-00-00 00:00:00 not null,
Primary key (infoid)
);


Here, DATEANDTIME is the standard date format, and then I want to query the records within five days. below is the SQL query statement
$ SQL = "select * from infomess where to_days (dateandtime) >=( to_days (now ()-5) order by infoid desc limit $ offset, $ psize ";

A where to_days (dateandtime) >=( to_days (now ()-5) is enough for another variable. here, 5 can be set as a variable.

Where to_days (dateandtime)> = (to_days (now ()-$ limitdays)

Then $ limitdays can be passed in GET mode (most of them are transmitted in GET mode)

Keep up with your PHP? Limitdays = 5. The same is true for 10 days. Change $ limitdasy to 10.

The above results are obtained by using the MYSQL function. all the above results have been tested. due to the rush of time, if there is any problem with the code, please post it with us. thank you.


Another friend said that the UNIX stamp was used to get such a result. Which of the following code has been written and posted for your reference and comparison, you can also test and determine whether PHP or MYSQL functions are highly efficient.


Http://www.oso.com.cn/forum/read.php? Theme_id = 7459 the main argument is concentrated in SELECT something FROM tab...

Contact Us

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.