pingcap / pingcap/tidb

Datetime output is different from Mysql

Open
#32,545 6 comments 0 reactions 1 assignee Claimed by @birdstorm View on GitHub
affects-5.0 affects-5.1 affects-5.2 affects-5.3 affects-5.4 may-affects-4.0 severity/moderate sig/execution type/bug
Dominant language
Go
Stars
40.5k
Forks
6.2k
PR merge metrics
PR metrics pending

Description

## Bug Report

Please answer these questions before submitting your issue. Thanks!

### 1. Minimal reproduce step (Required)

```sql
CREATE TABLE t1 ( a DATE,KEY ( a ) );
CREATE TABLE t2 ( a DATETIME,KEY ( a ) );
INSERT INTO t1 VALUES ('2009-09-22'), ('2009-09-22'), ('2009-09-22');
INSERT INTO t1 VALUES ('2009-09-23'), ('2009-09-23'), ('2009-09-23');
INSERT INTO t2 VALUES ('2009-09-22 12:00:00'), ('2009-09-22 12:00:00'),('2009-09-22 12:00:00');
INSERT INTO t2 VALUES ('2009-09-23 12:00:00'), ('2009-09-23 12:00:00'),('2009-09-23 12:00:00');
SELECT * FROM t1 WHERE a >= 20090923;
SELECT * FROM t2 WHERE a >= 20090923;
SELECT * FROM t2 WHERE a >= 20090923120000;
SELECT * FROM t1 WHERE a >= 20090923000000;
```
### 2. What did you expect to see? (Required)
```sql
mysql> CREATE TABLE t1 ( a DATE,KEY ( a ) );
Query OK, 0 rows affected (0.03 sec)

mysql> CREATE TABLE t2 ( a DATETIME,KEY ( a ) );
Query OK, 0 rows affected (0.02 sec)

mysql> INSERT INTO t1 VALUES ('2009-09-22'), ('2009-09-22'), ('2009-09-22');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t1 VALUES ('2009-09-23'), ('2009-09-23'), ('2009-09-23');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t2 VALUES ('2009-09-22 12:00:00'), ('2009-09-22 12:00:00'),('2009-09-22 12:00:00');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t2 VALUES ('2009-09-23 12:00:00'), ('2009-09-23 12:00:00'),('2009-09-23 12:00:00');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM t1 WHERE a >= 20090923;
+------------+
| a |
+------------+
| 2009-09-23 |
| 2009-09-23 |
| 2009-09-23 |
+------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM t2 WHERE a >= 20090923;
+---------------------+
| a |
+---------------------+
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
+---------------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM t2 WHERE a >= 20090923120000;
+---------------------+
| a |
+---------------------+
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
+---------------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM t1 WHERE a >= 20090923000000;
+------------+
| a |
+------------+
| 2009-09-23 |
| 2009-09-23 |
| 2009-09-23 |
+------------+
3 rows in set (0.00 sec)
```
### 3. What did you see instead (Required)
```sql

mysql> CREATE TABLE t1 ( a DATE,KEY ( a ) );
Query OK, 0 rows affected (0.11 sec)

mysql> CREATE TABLE t2 ( a DATETIME,KEY ( a ) );
Query OK, 0 rows affected (0.07 sec)

mysql> INSERT INTO t1 VALUES ('2009-09-22'), ('2009-09-22'), ('2009-09-22');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t1 VALUES ('2009-09-23'), ('2009-09-23'), ('2009-09-23');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t2 VALUES ('2009-09-22 12:00:00'), ('2009-09-22 12:00:00'),('2009-09-22 12:00:00');
Query OK, 3 rows affected (0.01 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> INSERT INTO t2 VALUES ('2009-09-23 12:00:00'), ('2009-09-23 12:00:00'),('2009-09-23 12:00:00');
Query OK, 3 rows affected (0.00 sec)
Records: 3 Duplicates: 0 Warnings: 0

mysql> SELECT * FROM t1 WHERE a >= 20090923;
+------------+
| a |
+------------+
| 2009-09-23 |
| 2009-09-23 |
| 2009-09-23 |
+------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM t2 WHERE a >= 20090923;
+---------------------+
| a |
+---------------------+
| 2009-09-22 12:00:00 |
| 2009-09-22 12:00:00 |
| 2009-09-22 12:00:00 |
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
+---------------------+
6 rows in set (0.00 sec)

mysql> SELECT * FROM t2 WHERE a >= 20090923120000;
+---------------------+
| a |
+---------------------+
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
| 2009-09-23 12:00:00 |
+---------------------+
3 rows in set (0.00 sec)

mysql> SELECT * FROM t1 WHERE a >= 20090923000000;
Empty set (0.00 sec)

```
### 4. What is your TiDB version? (Required)

```sql
| Release Version: v5.4.0
Edition: Community
Git Commit Hash: 55f3b24c1c9f506bd652ef1d162283541e428872
Git Branch: heads/refs/tags/v5.4.0
UTC Build Time: 2022-01-25 08:41:05
GoVersion: go1.16.4
Race Enabled: false
TiKV Min Version: v3.0.0-60965b006877ca7234adaced7890d7b029ed1306
Check Table Before Drop: false |
```

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.