MySQL date query operation copy

Source: Internet
Author: User

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

If it was 7 days ago, it would be the date_add function.

Select * from table name where date_sub (curdate (), interval 7 day)
<= Date (time field name)

Last 30 days ago

Select * from table name where date_sub (curdate (), interval 30 day)
<= Date (time field name)

This month

Select * from table name where date_format (time field name, '% Y % m') =
Date_format (curdate (), '% Y % m ')

Last month

Select * from table name where period_diff (date_format (now (), '% Y % m'
), Date_format (time field name, '% Y % m') = 1

// Converts the time to year, month, day, hour, minute, and second.
Select date_format (now (), '% Y % m % d % H % I % s ')
// Convert the time to year, month, and day
Select date_format (now (), '% Y % m % D ')
// At this time last year
Select date_add (now (), interval-1 year)
// At this time last month
Select date_add (now (), interval-1 month)
// At this time yesterday
Select date_add (now (), interval-1 day)
// One hour ago
Select date_add (now (), interval-1 hour)
// One minute ago
Select date_add (now (), interval-1 minute)
// One second ago
Select date_add (now (), interval-1 second)
// Yesterday (year, month, and day)
Select date_format (date_add (now (), interval 1 day), '% Y % m % D ')

// The first and last days of the last month
Select date_sub (date_format (now (), '% Y % m % D'), interval extract (day from now ()-1 day), interval 1 month );
Select date_sub (date_format (now (), '% Y % m % D'), interval extract (day from now () Day), interval 0 month );

// A string
Select date_format (date_add ('000000', interval 20 minute), '% Y % m % D ')
// Week
Select weekofyear (now ())
Select weekofyear ('20140901 ')

InMySQLThe string in the '20170901' and '20160901' formats is converted as the date type.

In MySQLOracleTo_char (Num, format) function of, so it is necessary to deal with the case that 0 is involved before the number.

For example, select left (Concat ('00'), @ num), 3) will display a string with three digits. When @ num = 1, 001 is displayed, and 123 is displayed.

Concat (Year (A. createtime), left (Concat ('0', weekofyear (A. createtime), 2 ))

AndFrom_unixtime, which can be converted to date by number

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.