lightdb MySQL兼容模式下timestampdiff函数支持首参无引号

文章目录

背景

在信创适配中,从MySQL迁移过来的程序使用了timestampdiff函数计算两个日期或时间之间差值,它可以使用年、季、月、日、周、日、时、分、秒、微秒等单位。

LightDB 23.4版本对该函数进行了增强,支持了MySQL的用法。timestampdiff首个参数可以带引号'MONTH'或不带引号MONTH

示例

准备数据

复制代码
create table date_table(
    c1 text, c2 text,
    c3 date, c4 date,
    c5 datetime, c6 datetime,
    c7 timestamp DEFAULT CURRENT_TIMESTAMP, c8 timestamp DEFAULT CURRENT_TIMESTAMP);

insert into date_table values (
    '1999-11-11 11:23:45', '2021-12-12 12:12:12',
    '1999-11-11 11:23:45', '2021-12-12 12:12:12',
    '1999-11-11 11:23:45', '2021-12-12 12:12:12',
    '1999-11-11 11:23:45', '2021-12-12 12:12:12'
);

insert into date_table values (
    '1999-11-11 11:23:45', null,
    null, '2021-12-12 12:12:12',
    null, null,
    '1999-11-11 11:23:45', '2021-12-12 12:12:12'
);

使用timestampdiff函数

复制代码
select timestampdiff ('MICROSECOND', c5, c6)  d1 , timestampdiff (MICROSECOND, c5, c6)  d2  from date_table;
       d1        |       d2        
-----------------+-----------------
 696991707000000 | 696991707000000
                 |                
(2 rows)

select timestampdiff ('SECOND', c5, c6)       d1 , timestampdiff (SECOND, c5, c6)       d2  from date_table;
    d1     |    d2     
-----------+-----------
 696991707 | 696991707
           |          
(2 rows)

select timestampdiff ('MINUTE', c5, c6)       d1 , timestampdiff (MINUTE, c5, c6)       d2  from date_table;
    d1    |    d2    
----------+----------
 11616528 | 11616528
          |         
(2 rows)

select timestampdiff ('HOUR', c5, c6)         d1 , timestampdiff (HOUR, c5, c6)         d2  from date_table;
   d1   |   d2   
--------+--------
 193608 | 193608
        |       
(2 rows)

select timestampdiff ('DAY', c5, c6)          d1 , timestampdiff (DAY, c5, c6)          d2  from date_table;
  d1  |  d2  
------+------
 8067 | 8067
      |     
(2 rows)

select timestampdiff ('WEEK', c5, c6)         d1 , timestampdiff (WEEK, c5, c6)         d2  from date_table;
  d1  |  d2  
------+------
 1152 | 1152
      |     
(2 rows)

select timestampdiff ('MONTH', c5, c6)        d1 , timestampdiff (MONTH, c5, c6)        d2  from date_table;
 d1  | d2  
-----+-----
 265 | 265
     |    
(2 rows)

select timestampdiff ('QUARTER', c5, c6)      d1 , timestampdiff (QUARTER, c5, c6)      d2  from date_table;
 d1 | d2 
----+----
 88 | 88
    |   
(2 rows)

select timestampdiff ('YEAR', c5, c6)         d1 , timestampdiff (YEAR, c5, c6)         d2  from date_table;
 d1 | d2 
----+----
 22 | 22
    |   
(2 rows)

总结

根据上述示例可以知道本次增强只是传参方式做了新的支持,函数本身行为并不发生改变。

相关推荐
Elastic 中国社区官方博客5 分钟前
Elasticsearch:使用推理端点及语义搜索演示
大数据·数据库·人工智能·elasticsearch·搜索引擎·ai·全文检索
武子康1 小时前
Java-143 深入浅出 MongoDB NoSQL:MongoDB、Redis、HBase、Neo4j应用场景与对比
java·数据库·redis·mongodb·性能优化·nosql·hbase
豆沙沙包?2 小时前
2025年--Lc171--H175 .组合两个表(SQL)
数据库·sql
麋鹿原2 小时前
Android Room 数据库之数据库升级
数据库·kotlin
GanGuaGua4 小时前
MySQL:表的约束
数据库·mysql
Li zlun5 小时前
MySQL 性能监控与安全管理完全指南
数据库·mysql·安全
养生技术人5 小时前
Oracle OCP认证考试题目详解082系列第48题
运维·数据库·sql·oracle·database·开闭原则·ocp
海阳宜家电脑6 小时前
Lazarus使用TSQLQuery更新的一点技巧
数据库·lazarus·tsqlquery
丨我是张先生丨6 小时前
SQLSERVER 查找存储过程中某个变量
数据库
感谢地心引力7 小时前
【Python】基于 PyQt6 和 Conda 的 PyInstaller 打包工具
数据库·python·conda·pyqt·pyinstaller