首页 > 数据库 > MySQL > 一个在使用方法上的低级错误(MySQL场景)
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)
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)
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)
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)
来自 “ ITPUB博客 ” ,链接:https://blog.itpub.net/637517/viewspace-3107204/ ,如需转载,请注明出处,否则将追究法律责任。