MySQL Time processing

Source: Internet
Author: User
Tags add time

Very many times. When we are querying the MySQL database, we want to process the time, for example, format or other operation, this way to avoid the processing. And MySQL also has a lot of time aspects of the processing function, today simple to do a small summary, for everyone to participate in the test.

First, the comparison function of time. This is another article that everyone has participated in, here no longer tells: http://blog.csdn.net/fanxl10/article/details/44172569

Let's say we want to add time or reduce processing. We can use the Date_add () function to pass in two parameters, the first argument is the time to change, the second is the value to change, for example, we want to add a day. So the number of participants is: INTERVAL 1 day, assuming that you want to reduce the number of days, then the argument is INTERVAL-1, passing in a negative value can be, that is to say we want to add one months, that is INTERVAL 1 month. And so on, adding a year is interval 1. Adding one hours is interval 1 HOUR. It must be clear how you should use it here. Here's a look at the example:

Check start time and add a day:

SELECT START, Date_add (START, INTERVAL 1 day) as Addstartfrom ts_tickeywhere id= ' 373 '

Find the results such as the following:


Suppose we want to add the start time of the database to the day. It's also very easy:

UPDATE ts_tickey SET start=date_add (START, INTERVAL 1 day) WHERE id= ' 373 '
Other use methods let's try it on our own. There's basically nothing to say.


The other one is the MySQL time Format function date_format (), as well, the transmission of two parameters. The first number of parameters is the time to format. The formatted format of the second parameter, for example:

Date_format (A.start, '%y-%m-%d ') as Start
This is the format of start time into YYYY-MM-DD format.

Or it can be formatted into seconds and minutes:

Date_format (a.uploaddate, '%h:%i:%s ')



MySQL Time processing

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.