MYSQL数据库转DM达梦数据库函数替换及注意事项

1、调整 IF函数为 case 函数

MYSQL:  IF(conditionvalue_if_truevalue_if_false

if(a.class_sort_code = '0301',(select group_concat(sku_attr_id) sku_Attr from a_sku_attr_rela WHERE model_id = a.model_id order by sku_attr_id),'') sku_attr

  DM:    case when  condition then value_if_true else  value_if_false end  
CASE WHEN a.class_sort_code = '0301' THEN (select wm_concat(sku_attr_id) sku_Attr from a_sku_attr_rela WHERE model_id = a.model_id order by sku_attr_id) ELSE '' END sku_attr

2、调整聚合函数 GROUP_CONCAT 为  wm_concat  (注 : 直接替换即可

MYSQL:group_concat( [distinct] 要连接的字段 [order by 排序字段 asc/desc ] [separator '分隔符'] )

  示例-表A :select name ,age,sex ,favorite from student      ---操作合并示例  favorite 字段  【favorite 字段值:1、篮球,2、足球,3、篮球,4、游泳】

    MYSQL: select Group_Concat(favorite) from student where name='zhangsan';       ----最终结果 favorite 中字段以【,】 连接 结果示例:篮球,足球,篮球,游泳

    去重:select Group_Concat(DISTINCT  facorite) from student where name='zhangsan';   ----最终结果 favorite 中字段以【,】 连接 结果示例:篮球,足球,游泳

    去重后以【;】连接:select Group_Concat(DISTINCT  facorite SEPARATOR ';')  from student where name='zhangsan'; ----最终结果 favorite 中字段以【;】 连接 结果示例:篮球;足球;游泳

DM  WM_COUNT

示例(以MySQL中sql表示例):select name ,age,sex ,favorite from student      ---操作合并示例  favorite 字段  【favorite 字段值:1、篮球,2、足球,3、篮球,4、游泳】

    DM: select WM_Concat(favorite) from student where name='zhangsan';       ----最终结果 favorite 中字段以【,】 连接 结果示例:篮球,足球,篮球,游泳

    去重:select WM_Concat(DISTINCT  facorite) from student where name='zhangsan';   ----最终结果 favorite 中字段以【,】 连接 结果示例:篮球,足球,游泳

    去重后以【;】连接:select  replace(WM_Concat(DISTINCT  facorite),‘,’,‘,’  )from student where name='zhangsan'; ----最终结果 favorite 中字段以【;】 连接 结果示例:篮球;足球;游泳

 

备注 还有另一种方式可以做到上述效果

 DM    listagg

字符串拼接函数,由于wm_concat是只能以逗号结尾进行分割,在实际应用中有些许不变,因此又写了一个函数能够灵活变通。

格式:<LISTAGG>(<参数>[,<参数>]) WITHIN GROUP (<ORDER BY 项>)

select
    TEMPLATE_ID,
    listagg(template_content,';') WITHIN  group (order by TEMPLATE_ID)
    from SMS_TEMPLATE_ITEM
group by TEMPLATE_ID;

3、时间计算函数  DATE_ADD()

--MYSQL DATEADD(datepart,n,date): 向指定的日期date加上n个datepart指定的时间段

select DATE_ADD(sysdate(), INTERVAL 1 YEAR);
--2024-07-03 14:57:54
select DATE_SUB(sysdate(), INTERVAL 1 YEAR);
--2022-07-03 14:57:54

select translate(to_char(sysdate(), 'yyyy-mm#'),'-#','年月')
--2023年07月

SELECT   date_format(now(),'%Y-%m-%d')  
-- 2023-07-03
 

--DM     ADD_DAYS(data,n)  date是日期类型(DATE)或时间戳类型(TIMESTAMP),返回值为日期类型(DATE)。     +n天  : ADD_MONTHS(date,n)、 ADD_WEEKS(date,n)、

select TIMESTAMPADD(SQL_TSI_YEAR,1,sysdate());
-- 时间日期计算 2024-07-03 14:57:56.000000

SELECT date_format(now(),'%Y-%m-%d')
-- 时间格式化 2023-07-03

select DATEDIFF( DAY ,sysdate(),'2024-05-01');
-- 返回时间区间 303

 

4、DM的convert()函数中的type在前, value在后,而MySQL数据库中convert()函数则恰恰相反

建议用 cast 用法基本一致;记得检查数据转换前的类型与后续类型是否可以互转

5、分析函数有 distinct 的时候,不允许 order by 一起使用;

6、DM数据库 字段/表名称用 ""   不允许 `` 且  格式化时指定格式用 ''

 

 

 参考网址:

达梦官网:  达梦技术文档 (dameng.com)

字段列转行及去重: MySQL、DM 行转列及字段去重(Group_Concat()) - 天為 - 博客园 (cnblogs.com)

DM时间函数: (213条消息) DM日期时间函数_dm时间段拆分_zcn126的博客-CSDN博客

 

posted @ 2023-07-03 15:21  不良徐某  阅读(3486)  评论(0编辑  收藏  举报