SQL query statement used to query the content of a specified date and time in mysql

Source: Internet
Author: User
Tags mysql tutorial

If the month is the current month minus the time you want to calculate, for example, to query all records in the database for the first three months from today, our statement is as follows: mktime (date (\ 'H \ '), date (\' I \ '), date (\'s \'), date (\'m \ ')-3, date (\ 'd \ '), date (\ 'y \'); mktime (date (\ 'H \ ') within seven days \'), date (\ 'I \'), date (\'s \ '), date (\'m \'), date (\ 'd \ ')-7, date (\ 'y \'));

Mysql tutorial query specified date and time content SQL query statement

In the database tutorial design, you must note that the time field is preferably int (4). In this way, when saving in the database, it is a digital Date and Time cut, we can use the mktime function to find the time cut of the current date and add or subtract it. Let's look at the instance.

$ Time = time ();
// One month

$ LastMonth = mktime (date ('H'), date ('I'), date ('s'), date ('M')-1, date ('D'), date ('y '));
$ Where. = "dtime <$ lastMonth ";

// Three months

$ LastThreeMonth = mktime (date ('H'), date ('I'), date ('s'), date ('M')-3, date ('D'), date ('y '));
$ Where. = "dtime <$ lastThreeMonth ";

$ SQL = "select * from testtable". $ where

/*
The principle is:
If the month is the current month minus the time you want to calculate, for example, to query all records in the database for the first three months from today, our statement is as follows: mktime (date ('H'), date ('I'), date ('s'), date ('M')-3, date ('D '), date ('y '));
Within seven days: mktime (date ('H'), date ('I'), date ('s'), date ('M'), date ('D ') -7, date ('y '));
In an hour: mktime (date ('H')-1, date ('I'), date ('s'), date ('M '), date ('D'), date ('y '));
Other methods are the same.

Source www. bKjia. c0m
*/

Related Article

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.