GBase 8a
其他
文章

DATE和TIMESTAMP时间类型在WHERE条件中的写法

发表于2024-12-30 20:02:4749次浏览2个评论

在按时间戳进行查询时有时会执行DATETIME函数进行格式化,经验证此方式无法执行,语法错误

gbase> select * from tab where birthday between datetime'2024-12-30 00:00:00' and datetime'2024-12-30 23:59:59';
ERROR 1064 (42000): You have an error in your SQL syntax; check the manual that corresponds to your GBase server version for the right syntax to use near ''2024-12-30 00:00:00' and datetime'2024-12-30 23:59:59'' at line 1

 

经验证去掉DATETIME关键字可以直接进行查询

gbase> select * from tab where birthday between '2024-12-30 00:00:00' and '2024-12-30 23:59:59'; 
+------+------+---------------------+
| id   | name | birthday            |
+------+------+---------------------+
|    3 | C    | 2024-12-30 19:59:33 |
|    4 | D    | 2024-12-30 19:59:33 |
|    7 | G    | 2024-12-30 19:59:33 |
|    8 | H    | 2024-12-30 19:59:33 |
|    9 | I    | 2024-12-30 19:59:33 |
|    1 | A    | 2024-12-30 19:59:32 |
|    2 | B    | 2024-12-30 19:59:32 |
|    5 | E    | 2024-12-30 19:59:33 |
|    6 | F    | 2024-12-30 19:59:33 |
|    0 | J    | 2024-12-30 19:59:34 |
+------+------+---------------------+
10 rows in set (Elapsed: 00:00:00.03)
 

评论

登录后才可以发表评论
用户头像
levvel发表于 5个月前
太有道理了!
GBase用户51887发表于 2个月前
学会了