SQL format based on time query

Source: Internet
Author: User
Tags mysql query
Products on-line within 24 hours:
SELECT * FROM goods
WHERE date_sub (Curdate (), INTERVAL 1 day) <= Add_time;
Today's online Products:
SELECT * FROM goods where to_days (add_time) =to_days (now ());
====== Reference =========
MySQL query today, yesterday, 7 days, nearly 30 days, this month data

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;

/*datediff (now (), From_unixtime (' Time field name ')) = 1; Time field Storage Type is timestamp */

7 days

SELECT * from ' table name ' where Date_sub (Curdate (), INTERVAL 7 day) <= Date (Time field name);

/*datediff (now (), From_unixtime (' Time field name ')) = 7; Time field Storage Type is timestamp */

Nearly 30 days

SELECT * FROM table name where Date_sub (Curdate (), INTERVAL Day) <= Date (Time field name);

Month

SELECT * FROM table name where Date_format (Time field name, '%y%m ' = Date_format (Curdate (), '%y%m ';

SQL format based on time query

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.