MySQL time date segmented query SQL statement

Source: Internet
Author: User

The Mktime function in PHP is queried by time, in fact, in addition to this solution, there are also functions specifically for date and time in MySQL, including date (), Date_add (), Date_sub (), Date_format (), and so on.

The code is as follows Copy Code

Orders in the last three months, the SQL statement is as follows:
$sql = "SELECT * from Shopping_order WHERE dttime>=date_sub (now (), INTERVAL 3 MONTH)";

If you are querying for orders within the last 15 days, the SQL statement is as follows:
$sql = "SELECT * from Shopping_order WHERE dttime>=date_sub (now (), INTERVAL)";


mktime function

The code is as follows Copy Code

function to query the order in the last three months, the SQL statement is as follows:
$bgdte =date ("y-m-d h:i:s", Mktime (Date (' H '), date (' I '), date (' s '), date (' m ') -3,date (' d '), date (' Y '));
$sql = "SELECT * from Shopping_order WHERE dttime>= '". $bgdte. "'";

If you are querying for orders within the last 15 days, the SQL statement is as follows:
$bgdte =date ("y-m-d h:i:s", Mktime (Date (' H '), date (' I '), date (' s '), date (' m '), date (' d ') -15,date (' Y '));
$sql = "SELECT * from Shopping_order WHERE dttime>= '". $bgdte. "'";

SQL by month group and monthly grouped query statement

The code is as follows Copy Code

GROUP BY Month:
Select Month (from_unixtime) from table_name GROUP by month (From_unixtime (time))
Grouped by Month:
Select Date_format (From_unixtime (Time), "%y-%m") from Tcm_fund_list Group by Date_format (From_unixtime (Time), "%y-%m")
Where time is an integer such as: 1159718400 represents 2006-10-02


query Today, yesterday, 7 days, nearly 30 days, month, last month data

  code is as follows copy code

     Today
    select * from table name where To_days (Time field name) = To_days (now ());
    yesterday     SELECT * FROM table name WHERE to_days (now ()) –to_days (Time field name) <= 1
    7 days
&NB sp;   SELECT * FROM table name where Date_sub (Curdate (), INTERVAL 7 day) <= DATE (Time field name)
    nearly 30 days     SELECT * from table name where Date_sub (Curdate (), INTERVAL Day) <= DATE (Time field name)
    This month
    SELECT * FROM table name WHERE date_format (Time field name, '%y%m ') = Date_format (Curdate (), '%y%m ')
&N bsp;   Previous Month
    SELECT * FROM table name WHERE Period_diff (now (), '%y%m '), Date_form At (Time field name, '%y%m ') =1

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.