Time issues in MySQL

Source: Internet
Author: User

1. Comparison of time

Using the time function to_date in Oracle is a habit. in Oracle, the addition and subtraction of time is also very simple. You can directly add and subtract time. There are many time functions in MySQL, which are very free.

MySQL cannot directly compare the time.> or <
You can only convert it to a number to compare the size.

In Oracle, trunc (sysdate, 'D') is often used to intercept to days or hours.
In MySQL, date_format () can be used instead. Returns a string in the time format, which is also a time string.
In MySQL, strings that conform to the time format can be treated as the date type.
From today to day,
Mysql> select date_format (now (), '% Y % m % D ');

If the time is accurate, you need to convert the time to the unix_timestamp and the millisecond value to compare the size.

Unix_timestampFor example:

Select * From projectmanager
Where (unix_timestamp (now ()-unix_timestamp ('2017-4-10 21:14:20 ')> 0;

Date calculation:

1. The time addition and subtraction function is
:
Add adddate () and subtract subdate ();
Select subdate (now (), interval 60 day); time 60 days ago
Select adddate (now (), interval 60 day); 60 days later

2. If the time difference is compared, you can add or subtract the value as follows:

1) query data that is 25 minutes earlier than the current time Select now ()-interval 25 minute; 2) query data of the current time of 25 minutes Select now () + interval 5 minute; 3. addtime addition and subtraction of time: When we are giving now () +-a time, we should actually understand it as follows: + 1/+ 01: add 1 second + 101/+ 0101: Add 1 minute 1 second + 10101/+ 010101: Add 1 hour 1 minute 1 second + 1010101/+ 01010101: add 1 day 1 minute 1 second + 101010101/+ 0101010101: Add 1 day 1 minute 1 second + 1101010101/+ 010101010101: Add 1 year January 1 day 1 minute 1 second, it should be noted that the year may be four digits (if there is no high position, it will be zero): 00010101010101 so for the problem of the front side: Pull the records added in the past three hours, you can write the SQL statement as follows: Select ID, title, content, addtime from tablename_tbl where addtime> = now ()-30000 and addtime <= now () 4. Another method of date_add and date_sub is provided by MySQL: Date_add (Add specified time interval to date), date_add (date, interval expr type)  Date_sub (Subtract the specified time interval from the date), date_sub (date, interval expr type) (similar to date_add)

Select ID, title, content, addtime from tablename_tbl where addtime> = date_sub (now, interval 3 hour) and addtime <now () Type values: microsecond, second, minute, hour, day, week, month, quarter, year, second_microsecond, hour, hour_minute, day_microsecond, day_second, day_minute, day_hour

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.