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)

总结

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

相关推荐
2401_831501731 小时前
Linux之Zabbix分布式监控篇(二)
数据库·分布式·zabbix
秋林辉2 小时前
Jfinal+SQLite处理 sqlite数据库执行FIND_IN_SET报错
jvm·数据库·sqlite
巴里巴气5 小时前
MongoDB复杂查询 聚合框架
数据库·mongodb
scheduleTTe8 小时前
SQL增查
数据库·sql
浮生带你学Java8 小时前
2025Java面试题及答案整理( 2025年 7 月最新版,持续更新)
java·开发语言·数据库·面试·职场和发展
期待のcode9 小时前
图片上传实现
java·前端·javascript·数据库·servlet·交互
小毛驴8509 小时前
redis 如何持久化
数据库·redis·缓存
吗喽1543451889 小时前
用python实现自动化布尔盲注
数据库·python·自动化
hbrown10 小时前
Flask+LayUI开发手记(十一):选项集合的数据库扩展类
前端·数据库·python·layui
云资源服务商12 小时前
探索阿里云DMS:解锁高效数据管理新姿势
数据库·阿里云·oracle·云计算