The difference between timestamp and sqltimestamp calculated by SQL.

Source: Internet
Author: User
Tags mysql functions

The difference between timestamp and sqltimestamp calculated by SQL.

SQL Calculation of timestamp difference

Overview

Sometimes we need to find some records by time, for example, calculate the record one hour before the sales time.
We can usually use the timestampdiff function of MYSQL to do this, but we cannot use the index. If the data volume is large, it will cause slow queries.

Use the code to calculate the time and then pass it to the SQL

We can use the JAVA code to calculate the time first and then pass it to the SQL statement to avoid using MYSQL functions.

Public long xxxx (long sellTimeFrom) {Calendar calendar = Calendar. getInstance (); calendar. setTime (new Date (sellTimeFrom); calendar. set (Calendar. HOUR_OF_DAY, calendar. get (Calendar. HOUR_OF_DAY)-1); return calendar. getTime (). getTime ();}

In this way, you can calculate the first hour of the sales time. Then input the SQL statement and write it into the code slice statement. In this way, if the sales time field has an index, you can use the index.

Thank you for reading this article. I hope it will help you. Thank you for your support for this site!

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.