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)

总结

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

相关推荐
一个数据大开发2 小时前
如何将excel数据快速导入数据库
数据库·excel
一介草民丶4 小时前
Mysql | 主从复制的工作机制
数据库·mysql·oracle
酱学编程7 小时前
redis 延迟双删
数据库·redis·缓存
xujiangyan_9 小时前
MySQL的半同步模式
数据库·git·mysql
飞翔沫沫情9 小时前
《MySQL 5.7.44审计合规实践:插件集成与日志分割自动化方案》
数据库·mysql·mysql审计
MXsoft6189 小时前
云原生运维在 2025 年的发展蓝图
运维·服务器·数据库
不辉放弃10 小时前
SQL 主键(Primary Key)
数据库·sql·oracle
qq_3392822310 小时前
PostgreSQL-常用命令
数据库·postgresql·oracle
沸材11 小时前
Redis——实现消息队列
数据库·redis·消息队列
しかし11811411 小时前
C语言队列的实现
c语言·开发语言·数据结构·数据库·经验分享·链表