原创

达梦数据库-语法-使用总结

1、COALESCE 相当于 ifnull

报错场景1:

SELECT COALESCE(SUM(querenshuliangkg), 0) AS total 
FROM table WHERE disable != 1 AND shenbaoniandu = '2025-01-01 00:00:00' \G

`querenshuliangkg` varchar(20) default ''

先将sum结果转成double,再对doule判null
SELECT COALESCE((CAST(SUM(querenshuliangkg) as double)),0) as total 
FROM table WHERE disable != 1 AND shenbaoniandu = '2025-01-01 00:00:00' \G

2、CASE WHEN 相当于 if(x,y,x)

select CASE case_value
    WHEN when_value THEN
        statement_list
    ELSE
        statement_list
END ;

3、group by;

        字段必须在select 列,且必须使用as ,哪怕as 自己
        如果有order by 同上。测试,如果同时存在就有矛盾,select 加了id,那group by 也要加上id,这就不对了

4、达梦不支持双引号

5、longtext 转 float 并做加法

    q1886、q23bb  是longtext

    select (CAST(CAST(q1886 AS VARCHAR) AS FLOAT) + CAST(CAST(q23bb AS VARCHAR) AS FLOAT)) AS zhibiaopaifangliang 
    FROM table where disable != 1 \G
正文到此结束
本文目录