news 2026/5/1 7:09:04

一个在使用方法上的低级错误(MySQL场景)

作者头像

张小明

前端开发工程师

1.2k 24
文章封面图
一个在使用方法上的低级错误(MySQL场景)

首先申明这个技术含量不高

  • 初始化问题数据。注意这里的t代表时间,数据类型是字符串。
  • 为什么用字符串来表示时间?那这就是问题所在。当初创建的人不懂。
  • 这个回答不知道满意吗?
mysql> create table t1 (id int,t varchar(20)); Query OK, 0 rows affected (0.02 sec) mysql> insert into t1 values (1,'2026-01-01 01:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (2,'2026-01-01 10:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (3,'2026-01-02 02:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (4,'2026-01-03 02:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (5,'2026-01-04 08:00:00'); Query OK, 1 row affected (0.00 sec) mysql> insert into t1 values (6,'2026-01-02 00:00:00'); Query OK, 1 row affected (0.01 sec) mysql> select * from t1; +------+---------------------+ | id | t | +------+---------------------+ | 1 | 2026-01-01 01:00:00 | | 2 | 2026-01-01 10:00:00 | | 3 | 2026-01-02 02:00:00 | | 4 | 2026-01-03 02:00:00 | | 5 | 2026-01-04 08:00:00 | | 6 | 2026-01-02 00:00:00 | +------+---------------------+ 6 rows in set (0.00 sec)
  • 如果查询小于等于1月2日的。注意我写了等于,但是结果是没有1月2日。这也可以理解。字符串就这样定义,从第一个字符开始算。哪怕是1月2日0点0分0秒也不属于范围。
  • 只有条件是小于1月3日,才能看到1月2日。
mysql> select * from t1 where t<='2026-01-02'; +------+---------------------+ | id | t | +------+---------------------+ | 1 | 2026-01-01 01:00:00 | | 2 | 2026-01-01 10:00:00 | +------+---------------------+ 2 rows in set (0.00 sec) mysql> select * from t1 where t<='2026-01-03'; +------+---------------------+ | id | t | +------+---------------------+ | 1 | 2026-01-01 01:00:00 | | 2 | 2026-01-01 10:00:00 | | 3 | 2026-01-02 02:00:00 | | 6 | 2026-01-02 00:00:00 | +------+---------------------+ 4 rows in set (0.00 sec)
  • 然后同样的表把字段类型换一下。把时间字段给于正经的时间类型。(不过时间精度只到天,因为有的系统场景只要到天就行了。前面模拟的时分秒的仅仅是为了展示说明0点的边界差异)
  • 可以看到,这个小于等于时候是能把1月2日时间查出来的。就这一点的差距,取数能查一天的数据量。
  • 我给别人演示,这就是他的问题所在。在Oracle中没这种问题。因为时间类型和字符类型的写法都不一样。
mysql> drop table t2; Query OK, 0 rows affected (0.01 sec) mysql> create table t2 as select * from t1; Query OK, 6 rows affected (0.03 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> alter table t2 modify t date; Query OK, 6 rows affected, 5 warnings (0.05 sec) Records: 6 Duplicates: 0 Warnings: 5 mysql> select * from t2; +------+------------+ | id | t | +------+------------+ | 1 | 2026-01-01 | | 2 | 2026-01-01 | | 3 | 2026-01-02 | | 4 | 2026-01-03 | | 5 | 2026-01-04 | | 6 | 2026-01-02 | +------+------------+ 6 rows in set (0.00 sec) mysql> select * from t2 where t<='2026-01-02'; +------+------------+ | id | t | +------+------------+ | 1 | 2026-01-01 | | 2 | 2026-01-01 | | 3 | 2026-01-02 | | 6 | 2026-01-02 | +------+------------+ 4 rows in set (0.00 sec)
  • 再换一下时间类型。这种场景下和第一张字符串时间取值几乎一致,区别是0点0分0秒的能查询的到。如果要1月2日所有的数据,条件还是要写小于1月3日。注意不要带=
mysql> create table t3 as select * from t1; Query OK, 6 rows affected (0.02 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> alter table t3 modify t datetime; Query OK, 6 rows affected (0.04 sec) Records: 6 Duplicates: 0 Warnings: 0 mysql> select * from t3; +------+---------------------+ | id | t | +------+---------------------+ | 1 | 2026-01-01 01:00:00 | | 2 | 2026-01-01 10:00:00 | | 3 | 2026-01-02 02:00:00 | | 4 | 2026-01-03 02:00:00 | | 5 | 2026-01-04 08:00:00 | | 6 | 2026-01-02 00:00:00 | +------+---------------------+ 6 rows in set (0.00 sec) mysql> select * from t3 where t<='2026-01-02'; +------+---------------------+ | id | t | +------+---------------------+ | 1 | 2026-01-01 01:00:00 | | 2 | 2026-01-01 10:00:00 | | 6 | 2026-01-02 00:00:00 | +------+---------------------+ 3 rows in set (0.00 sec)

错误是低级的,再结合数据类型就有点绕。

版权声明: 本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若内容造成侵权/违法违规/事实不符,请联系邮箱:809451989@qq.com进行投诉反馈,一经查实,立即删除!
网站建设 2026/4/30 22:15:18

Java_ElasticSearch(ES)——分布式搜索引擎

介绍&#xff1a; Elasticsearch是一个开源的分布式搜索和分析引擎&#xff0c;最初由Elastic公司开发。它构建在Apache Lucene搜索引擎库之上&#xff0c;提供了一个强大的全文搜索和分析引擎&#xff0c;它结合kibana、Logstash、Beats&#xff0c;是一整套技术栈&#xff0…

作者头像 李华
网站建设 2026/4/29 17:39:07

泰和昌商城接口自动化项目框架介绍

一、项目介绍基于PythonpytestsqlalchemyrequestsallurejsonpathyamlJenkinsLinux基于PythonpytestsqlalchemyrequestsallurejsonpathyamlJenkinsLinux技术栈搭建的泰和昌商城接口自动化测试项目&#xff0c;针对在线购物商城的核心业务流程开展接口自动化验证。 该项目覆盖泰和…

作者头像 李华
网站建设 2026/4/24 10:43:35

InfluxDB 用户管理与Token配置实用指南

📖 快速入门 本指南将帮助您快速掌握 InfluxDB 的用户管理和Token配置,包含大量实用示例和最佳实践。 🎯 核心概念 基础架构 InfluxDB 实例 ├── 组织 (Organization) │ ├── 用户 (Users) │ ├── 存储桶 (Buckets) │ └── 权限令牌 (Tokens) └──…

作者头像 李华
网站建设 2026/4/25 9:02:06

救命神器!自考必看TOP9 AI论文网站测评与推荐

救命神器&#xff01;自考必看TOP9 AI论文网站测评与推荐 2026年自考论文写作工具测评&#xff1a;为何需要这份榜单&#xff1f; 随着人工智能技术的不断进步&#xff0c;AI论文写作工具在学术领域的应用越来越广泛。对于自考学生而言&#xff0c;撰写高质量论文不仅是毕业的必…

作者头像 李华