登录
首页 >  数据库 >  MySQL

postgresql怎么兼容MySQL if函数

来源:亿速云

时间:2023-05-12 21:13:38 431浏览 收藏

今日不肯埋头,明日何以抬头!每日一句努力自己的话哈哈~哈喽,今天我将给大家带来一篇《postgresql怎么兼容MySQL if函数》,主要内容是讲解等等,感兴趣的朋友可以收藏或者有更好的建议在评论提出,我都会认真看的!大家一起进步,一起学习!

    postgresql兼容MySQL if函数

    if函数说明

    在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:

    IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值

    postgresql自定义if函数兼容

    create or replace function if(bln boolean,inValue1 anyelement,inValue2 anyelement)
    returns anyelement as
    $$
    begin
    if bln=true then
       return inValue1;
    else
       return inValue2;
    end if;
    end;
    $$
    language plpgsql;
    
    create or replace function if(bln boolean,inValue1 numeric,inValue2 numeric)
    returns numeric as
    $$
    begin
    if bln=true then
       return inValue1;
    else
       return inValue2;
    end if;
    end;
    $$
    language plpgsql;
    
    create or replace function if(bln boolean,inValue1 numeric,inValue2 text)
    returns text as
    $$
    begin
    if bln=true then
       return inValue1;
    else
       return inValue2;
    end if;
    end;
    $$
    language plpgsql;

    mysql、oracle、postgresql兼容适配

    sql使用区别

    1. dual表

    oracle独有的表,目的是限制sql语句结构完整

    select (select * from table_name where age = 20) t from dual

    mysql和pgsql没有这张表,可以直接去掉

    select (select * from table_name where age = 20) t

    2. 布尔类型

    oracle和mysql没有boolean类型,可使用number(int)或char代替

    pgsql中有bool类型,数字和字符自动转换为boolean类型(0→f、1→t、no→f、yes→t)

    3. update表别名

    pgsql不适用,mysql 和 oracle支持

    update table_name t set t.name = 'abc' where id = 1

    4. 字符串传值

    pgsql 、oracle 仅支持单引号

    select * from table_name where name = 'abc'

    mysql 单引号/双引号都支持

    select * from table_name where name = "abc"

    5. 批量插入

    mysql、pgsql批量插入

    insert into table_name() values()

    oracle批量插入

    insert all into table_name() values()

    mybatis兼容不同数据库

    使用if标签判断_databaseId,分别适配不同的数据库,具体代码如下:

    
        
            insert into table_name 
            ()
            values
            
                ()
            
        
        
            insert all
            
                into table_name 
                ()
                values ()
            
            select * from dual
        
    
     
    
        id,name,age,gender
    
    
        #{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, 
        #{item.age,jdbcType=INTEGER},#{item.gender,jdbcType=INTEGER}
    

    今天带大家了解了的相关知识,希望对你有所帮助;关于数据库的技术知识我们会一点点深入介绍,欢迎大家关注golang学习网公众号,一起学习编程~

    声明:本文转载于:亿速云 如有侵犯,请联系study_golang@163.com删除
    相关阅读
    更多>
    最新阅读
    更多>
    课程推荐
    更多>