code学习

mysql5.7和8.0及mariadb时间条件查询结果差别

mysql5.7和8.0及mariadb时间条件查询结果差别

一、环境

二进制版本mariadb10-2.10,mariadb10-2.17 mysql-5.7.22 mysql5.7.32 mysql-8.0.28

环境sql_mode都按照如下:

sql_mode='STRICT_TRANS_TABLES,ERROR_FOR_DIVISION_BY_ZERO,NO_AUTO_CREATE_USER,NO_ENGINE_SUBSTITUTION'
           

准备测试数据:

CREATE TABLE `t_user` (
 `id` bigint(11) unsigned NOT NULL AUTO_INCREMENT COMMENT '主键',
 `user_name` varchar(48) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '员工姓名/机构名称',
 `nick_name` varchar(100) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '昵称',
 `phone` varchar(15) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '员工手机号/机构手机号',
 `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '员工/机构邮箱 ',
 `employee_id` bigint(11) NOT NULL DEFAULT '0' COMMENT '人力员工id',
 `user_type` tinyint(1) NOT NULL DEFAULT '0' COMMENT '0:个人账号-默认,1:机构账号',
 `avatar` varchar(200) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '头像 ',
 `profile_bio` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL DEFAULT '' COMMENT '个性签名',
 `status` tinyint(1) NOT NULL DEFAULT '1' COMMENT '1:启用-默认,0:禁用',
 `create_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP COMMENT '创建时间',
 `update_time` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP COMMENT '更新时间',
 PRIMARY KEY (`id`),
 KEY `idx_user_emp_id` (`employee_id`) USING BTREE,
 KEY `idx_user_email` (`email`) USING BTREE
) ENGINE=InnoDB  DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci COMMENT='用户表';

insert测试数据:
insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('小花','蓝天','13800000000','3056782@qq.com','2022-04-01 10:21:35');
insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('藏名','老兰','13800000000','3056782@qq.com','2022-04-02 10:21:35');
insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('藏文','阿刁','13800000001','3056782@qq.com','2022-04-03 10:21:35');

insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('杨文','娜扎','13800000002','3056782@qq.com','2022-04-03 10:21:35');
insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('王文','小扎','13800000005','3056782@qq.com','2022-04-04 11:21:35');
insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('李四','小扎','13800000225','3056782@qq.com','2022-04-05 11:21:35');
insert into t_user(`user_name`,`nick_name`,`phone`,`email`,`create_time`)value('赵六','小扎','1380033226','3056782@qq.com','2022-04-06 11:21:35');
           

二、具体测试过程

2.1、mysql版本为5.7.22和5.7.32

root@tidb04 16:50:  [test001]> select version();
+------------+
| version()  |
+------------+
| 5.7.22-log |
+------------+
1 row in set (0.00 sec)


查不出结果:
root@tidb04 16:50:  [test001]> select * from t_user where create_time > '2022-04-0400:00:00';
Empty set, 1 warning (0.00 sec)

root@tidb04 16:49:  [test001]> select * from t_user where create_time > concat(current_date,'00:00:00') ;
Empty set, 1 warning (0.00 sec)

root@tidb04 16:49:  [test001]> show warnings;
+---------+------+----------------------------------------------------------------------------------+
| Level   | Code | Message                                                                          |
+---------+------+----------------------------------------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 1 |
+---------+------+----------------------------------------------------------------------------------+
1 row in set (0.00 sec)

去掉空格,显示查询正确的结果:
root@tidb04 16:49:  [test001]> select * from t_user where create_time > concat(current_date,' 00:00:00') ;
+------+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id   | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+------+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| 2016 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 16:19:54 |
+------+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
1 row in set (0.00 sec)
root@tidb04 16:56:  [test001]> select * from t_user where create_time > '2022-04-04 00:00:00';
+------+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id   | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+------+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| 2016 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 16:19:54 |
+------+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
1 row in set (0.00 sec)

           

分析:在mysql5.7.32测试结果和mysql5.7.22版本是一致的 这个方式算是比较合理的

2.2、mysql8.0.28 测试

root@tidb05 21:28:  [test001]> select * from t_user where create_time > '2022-04-04 00:00:00';
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
|  5 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 16:33:51 |
|  6 | 李四      | 小扎      | 13800000225 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-05 11:21:35 | 2022-04-04 21:25:34 |
|  7 | 赵六      | 小扎      | 1380033226  | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-06 11:21:35 | 2022-04-04 21:25:42 |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
3 rows in set (0.00 sec)


root@tidb05 21:25:  [test001]> select * from t_user where create_time > concat(current_date,' 00:00:00') ;
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
|  5 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 16:33:51 |
|  6 | 李四      | 小扎      | 13800000225 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-05 11:21:35 | 2022-04-04 21:25:34 |
|  7 | 赵六      | 小扎      | 1380033226  | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-06 11:21:35 | 2022-04-04 21:25:42 |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
3 rows in set (0.00 sec)

去掉空格的话全部显示出来,得到的结果是错误的:
root@tidb05 21:25:  [test001]> select * from t_user where create_time > concat(current_date,'00:00:00') ;
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
|  1 | 小花      | 蓝天      | 13800000000 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-01 10:21:35 | 2022-04-04 16:32:31 |
|  2 | 藏名      | 老兰      | 13800000000 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-02 10:21:35 | 2022-04-04 16:32:42 |
|  3 | 藏文      | 阿刁      | 13800000001 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-03 10:21:35 | 2022-04-04 16:32:54 |
|  4 | 杨文      | 娜扎      | 13800000002 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-03 10:21:35 | 2022-04-04 16:33:30 |
|  5 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 16:33:51 |
|  6 | 李四      | 小扎      | 13800000225 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-05 11:21:35 | 2022-04-04 21:25:34 |
|  7 | 赵六      | 小扎      | 1380033226  | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-06 11:21:35 | 2022-04-04 21:25:42 |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
7 rows in set, 7 warnings (0.00 sec)


root@tidb05 21:29:  [test001]> show warnings;
+---------+------+----------------------------------------------------------------------------------+
| Level   | Code | Message                                                                          |
+---------+------+----------------------------------------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 1 |
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 2 |
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 3 |
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 4 |
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 5 |
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 6 |
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' for column 'create_time' at row 7 |
+---------+------+----------------------------------------------------------------------------------+
7 rows in set (0.00 sec)

           

2.3、10.2.10-MariaDB-和10.2.17-MariaDB测试

(Mon Apr  4 20:46:51 2022)[root@MySQL][test]>select version();
+---------------------+
| version()           |
+---------------------+
| 10.2.10-MariaDB-log |
+---------------------+
1 row in set (0.00 sec)

(Mon Apr  4 20:51:17 2022)[root@MySQL][test]>select * from t_user where create_time > '2022-04-04 00:00:00';
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
|  4 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 20:46:35 |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
1 row in set (0.00 sec)

(Mon Apr  4 20:51:31 2022)[root@MySQL][test]>select * from t_user where create_time > concat(current_date,' 00:00:00') ;
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
|  4 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 20:46:35 |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
1 row in set (0.00 sec)

**mariadb 缺少空格 整个表数据都出来了,得到的不是要查询的数据:这个有应该算mariadb的此版本的一个bug**
(Mon Apr  4 20:57:33 2022)[root@MySQL][test]>select * from t_user where create_time > concat(current_date,'00:00:00') ;
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
| id | user_name | nick_name | phone       | email          | employee_id | user_type | avatar | profile_bio | status | create_time         | update_time         |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
|  1 | 藏名      | 老兰      | 13800000000 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-02 10:21:35 | 2022-04-04 20:46:34 |
|  2 | 藏文      | 阿刁      | 13800000001 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-03 10:21:35 | 2022-04-04 20:46:34 |
|  3 | 杨文      | 娜扎      | 13800000002 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-03 10:21:35 | 2022-04-04 20:46:34 |
|  4 | 王文      | 小扎      | 13800000005 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-04 11:21:35 | 2022-04-04 20:46:35 |
|  5 | 李四      | 小扎      | 13800002225 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-05 11:21:35 | 2022-04-04 20:57:13 |
|  6 | 赵六      | hha       | 13800002225 | 3056782@qq.com |           0 |         0 |        |             |      1 | 2022-04-06 11:21:35 | 2022-04-04 20:57:31 |
+----+-----------+-----------+-------------+----------------+-------------+-----------+--------+-------------+--------+---------------------+---------------------+
6 rows in set, 1 warning (0.00 sec)

(Mon Apr  4 20:57:37 2022)[root@MySQL][test]>select current_date();
+----------------+
| current_date() |
+----------------+
| 2022-04-04     |
+----------------+
1 row in set (0.00 sec)

(Mon Apr  4 20:53:59 2022)[root@MySQL][test]>show warnings;
+---------+------+------------------------------------------------+
| Level   | Code | Message                                        |
+---------+------+------------------------------------------------+
| Warning | 1292 | Incorrect datetime value: '2022-04-0400:00:00' |
+---------+------+------------------------------------------------+
1 row in set (0.00 sec)
           

继续阅读