Pitfalls and solutions encountered in MySQL timestamp comparison query

Pitfalls and solutions encountered in MySQL timestamp comparison query

Pitfalls encountered in timestamp comparison queries

I remember that JD.com required MySQL to set update_time as timestamp and create_time as datetime when creating a table. Later, Alibaba's coding standards required that both must be of datetime type.

specification

The difference between timestamp and datetime is introduced in many places. Sometimes I wonder why JD.com requires update_time to be a timestamp? Is it because it takes up less space? Or is it only possible to set a default value for timestamp (on update current_timestamp)? Can't the default value datetime also be set? Later I searched on Baidu and found out that datetime support for setting default values ​​was only available in 5.7. JD.com may have such a requirement because the MySQL version used before was too low, and it also required update_time to be automatically updated.

Now a company also requires this, update_time is set to timestamp. As a result, I encountered a pit. A colleague found a very strange problem: why does the date comparison query have no results, but directly executing the SQL printed in the log can query the results? ? Why does this inconsistency occur? I have never encountered this before. Solving problems is always exciting.

describe

I tried it locally and it was indeed the case. There was no problem with the printed log, but it was the log that 'confused' us and made us feel very strange. I checked and found that the field being compared is update_time, which is of timestamp type. After being influenced by Alibaba's standards, I keenly felt that it should be a problem of type. So I searched on Baidu and found that it was a time zone problem. Just add the serverTimezone=GMT%2B8 parameter after the database connection URL. Of course, another way is to use datetime, which can avoid many pitfalls.

Why does this problem occur? This is because the time zone of the application server and the server where MySQL is deployed are inconsistent. This is why we see no problem with the print log, but no query results (the time seen in the log is the time zone of the local machine, but when the data is transferred to the MySQL server, it is the time in another time zone)

MySQL date also has this problem. . .

Timestamp query range problem

In MySQL, for example, if the update time is 2020-05-26, the query is update_time <= 2020-05-26, which cannot be found. It needs to be converted to DATE_FORMAT(info.up_time,'%Y-%m-%d') <= '2020-05-26'. The specific reason is unknown and needs further study.

The above is my personal experience. I hope it can give you a reference. I also hope that you will support 123WORDPRESS.COM.

You may also be interested in:
  • MySQL slow query pitfalls
  • Solving the abnormality of mysql datetime query
  • Why MySQL can ignore time zone issues when using timestamp?
  • SQL method for calculating timestamp difference

<<:  How to disable IE10's password clear text display and quick clear function

>>:  How to create a flame effect using CSS

Recommend

Four data type judgment methods in JS

Table of contents 1. typeof 2. instanceof 3. Cons...

Example code for implementing triangles and arrows through CSS borders

1. CSS Box Model The box includes: margin, border...

Solve the black screen problem after VMware installs Linux system and starts

1. Installation environment 1. HUAWEI mate x cpu ...

Notes on MySQL case sensitivity

Table of contents MySQL case sensitivity is contr...

How to install MySQL using yum on Centos7 and achieve remote connection

Centos7 uses yum to install MySQL and how to achi...

Font Treasure House 50 exquisite free English font resources Part 2

Designers have their own font library, which allo...

Steps to modify the MySQL database data file path under Linux

After installing the MySQL database using the rpm...

How to insert 10 million records into a MySQL database table in 88 seconds

The database I use is MySQL database version 5.7 ...

Nginx reverse proxy to go-fastdfs case explanation

background go-fastdfs is a distributed file syste...

MySQL v5.7.18 decompression version installation detailed tutorial

Download MySQL https://dev.mysql.com/downloads/my...

Vue realizes the whole process of slider drag verification function

Rendering Define the skeleton, write HTML and CSS...

Tips to prevent others from saving as my web page and copying my site

Nowadays, copying websites is very common on the I...

How to install mysql5.6 in docker under ubuntu

1. Install mysql5.6 docker run mysql:5.6 Wait unt...

HTML Tutorial: Collection of commonly used HTML tags (6)

Related articles: Beginners learn some HTML tags ...

Docker custom network container interconnection

Table of contents Preface –link Custom Network As...