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)

总结

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

相关推荐
阿华的代码王国14 分钟前
MySQL ------- 索引(B树B+树)
数据库·mysql
Hello.Reader42 分钟前
StarRocks实时分析数据库的基础与应用
大数据·数据库
执键行天涯44 分钟前
【经验帖】JAVA中同方法,两次调用Mybatis,一次更新,一次查询,同一事务,第一次修改对第二次的可见性如何
java·数据库·mybatis
yanglamei19621 小时前
基于GIKT深度知识追踪模型的习题推荐系统源代码+数据库+使用说明,后端采用flask,前端采用vue
前端·数据库·flask
工作中的程序员1 小时前
ES 索引或索引模板
大数据·数据库·elasticsearch
严格格1 小时前
三范式,面试重点
数据库·面试·职场和发展
微刻时光2 小时前
Redis集群知识及实战
数据库·redis·笔记·学习·程序人生·缓存
单字叶2 小时前
MySQL数据库
数据库·mysql
mqiqe2 小时前
PostgreSQL 基础操作
数据库·postgresql·oracle
just-julie2 小时前
MySQL面试题——第一篇
数据库·mysql