温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

postgresql怎么兼容MySQL if函数

发布时间:2023-03-21 14:26:36 来源:亿速云 阅读:84 作者:iii 栏目:开发技术

这篇文章主要介绍“postgresql怎么兼容MySQL if函数”,在日常操作中,相信很多人在postgresql怎么兼容MySQL if函数问题上存在疑惑,小编查阅了各式资料,整理出简单好用的操作方法,希望对大家解答”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 id="insertBatch" parameterType="java.util.List">
        <if test="_databaseId=='mysql' or _databaseId=='postgresql'">
            insert into table_name 
            (<include refid="insertBatchColumn"></include>)
            values
            <foreach collection="list" item="item" index="index" separator="," >
                (<include refid="insertBatchValue"></include>)
            </foreach>
        </if>
        <if test="_databaseId=='oracle'">
            insert all
            <foreach collection="list" item="item" index="index" separator="">
                into table_name 
                (<include refid="insertBatchColumn"></include>)
                values (<include refid="insertBatchValue"></include>)
            </foreach>
            select * from dual
        </if>
    </insert>
     
    <sql id="insertBatchColumn">
        id,name,age,gender
    </sql>
    <sql id="insertBatchValue">
        #{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, 
        #{item.age,jdbcType=INTEGER},#{item.gender,jdbcType=INTEGER}
    </sql>

    到此,关于“postgresql怎么兼容MySQL if函数”的学习就结束了,希望能够解决大家的疑惑。理论与实践的搭配能更好的帮助大家学习,快去试试吧!若想继续学习更多相关知识,请继续关注亿速云网站,小编会继续努力为大家带来更多实用的文章!

    向AI问一下细节

    免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

    AI