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)

总结

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

相关推荐
要开心吖ZSH17 小时前
软件设计师备考-(十四)数据库设计
java·数据库·软考·软件设计师
xrkhy17 小时前
阿里云对象存储OSS的使用
java·数据库·阿里云
少年攻城狮17 小时前
SpringAI系列---【如何把ChatClient集成向量数据库SimpleVectorStore并实现RAG?】
数据库
问道飞鱼18 小时前
【数据库相关】TxSQL新增数据库节点步骤
数据库·mysql·txsql·新增节点
张人玉18 小时前
SQLSERVER数据备份
数据库·oracle·sqlserver
Ka1Yan18 小时前
MySQL索引优化
开发语言·数据结构·数据库·mysql·算法
TDengine (老段)18 小时前
TDengine 时间函数 TIMETRUNCATE 用户手册
java·大数据·数据库·物联网·时序数据库·tdengine·涛思数据
Mors.19 小时前
dbeaver工具连接inceptor星环数据库
数据库
TG_yunshuguoji20 小时前
阿里云国际代理:阿里云的云数据库是什么?
服务器·数据库·安全·阿里云·云计算
huazhixuthink21 小时前
PostgreSQL三种关闭方式的区别
数据库·postgresql