MySQL query today, yesterday, nearly 7 days, nearly 30 days, this month, last month data method (excerpt)

Source: Internet
Author: User
Tags mysql query

MySQL query today, yesterday, nearly 7 days, nearly 30 days, this month, the last month of data method Analysis Summary:

There is an article table article, the time to store the article's add-on is the Add_time field, which is of type int (5), and now you need to query the total number of articles added today and sort by time from large to small, then the query statement is as follows:

SELECT * from ' article ' where Date_format (From_unixtime (' add_time '), '%y-%m-%d ') = Date_format (now (), '%y-%m-%d ');

Or:

SELECT * from ' article ' where To_days (Date_format (from_unixtime (' add_time '), '%y-%m-%d ')) = To_days (now ());

Assuming that the storage type of the Add_time field for the table above is a datetime type or timestamp type, the query statement can also be written as follows:

Query today's information records:

SELECT * from ' article ' where to_days (' add_time ') = To_days (now ());

Query yesterday's information record:

SELECT * from ' article ' where To_days (now ()) –to_days (' Add_time ') <= 1;

Check the information record for nearly 7 days:

Date (' Add_time ');

Check the information record for nearly 30 days:

Date (' Add_time ');

Query this month's information records:

SELECT * from ' article ' where Date_format (' add_time ', '%y%m ') = Date_format (Curdate (), '%y%m ');

Query the information record for the previous January:

SELECT * from ' article ' where Period_diff (Date_format (now (), '%y%m '), Date_format (' add_time ', '%y%m ')) = 1;

Take a look at some of the functions in the SQL statement above:

(1) To_days

Like its name, it is a Unix timestamp that converts a specific date or time string to a day, such as:

mysql> select to_days (' 2010-11-22 14:39:51 ');      02+--------------------------------+                                                       03| to_days (' 2010-11-22 14:39:51 ') |04+--------------------------------+ 05|                         734463 |06+--------------------------------+0708mysql> select to_days (' 2010-11-23 14:39:51 '); 09+--------------------------------+10| To_days (' 2010-11-23 14:39:51 ') |11+--------------------------------+12|                         734464 |13+--------------------------------+

It can be seen that the difference between 22nd and 23rd is that the number of conversions increased by 1, the granularity of the query is relatively coarse, and sometimes may not meet our query requirements, then we need to use a fine-grained query method Str_to_date function, the following will analyze the use of this function.

Remind:

(1) to_days () is not used for the value before the Gregorian calendar (1582), because the lost date is not taken into account when the calendars are changed. Therefore, the results of this function are not reliable for dates prior to 1582 (perhaps in other regions for the next year).
(2) The rule in MySQL date and time type is to convert the two-digit year value in the date to four-bit. So for ' 1997-10-07 ' and ' 97-10-07 ' will be considered the same date:

1mysql> select to_days (' 1997-10-07 '), To_days (' 97-10-07 '); 23-> 729669, 729669

(2) Str_to_date

This function can translate the string time completely, such as:

1mysql> select str_to_date ("2010-11-23 14:39:51", '%y-%m-%d%h:%i:%s '); 23+--------------------------------------------------------+4| Str_to_date ("2010-11-23 14:39:51", '%y-%m-%d%h:%i:%s ') |5+------------------------------------------------------- -+6| 2010-11-23 14:39:51                                    |7+--------------------------------------------------------+

The specific case actions are as follows:

Select Str_to_date (article. ' Add_time ', '%y-%m-%d%h:%i:%s ')  where str_to_date (article' add_time ', '%y-%m-%d%h:%i:%s ') >= ' 2012-06-28 08:00:00 ' and
Str_to_date (article. ' Add_time ', '%y-%m-%d%h:%i:%s ') <= ' 2012-06-28 09:59:59 ';

来源:Http://www.phpernote.com/mysql/350.html

MySQL query today, yesterday, nearly 7 days, nearly 30 days, this month, last month data method (excerpt)

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.