溫馨提示×

溫馨提示×

您好,登錄后才能下訂單哦!

密碼登錄×
登錄注冊×
其他方式登錄
點擊 登錄注冊 即表示同意《億速云用戶服務(wù)條款》

postgresql怎么兼容MySQL?if函數(shù)

發(fā)布時間:2023-03-21 14:26:36 來源:億速云 閱讀:113 作者:iii 欄目:開發(fā)技術(shù)

這篇文章主要介紹“postgresql怎么兼容MySQL if函數(shù)”,在日常操作中,相信很多人在postgresql怎么兼容MySQL if函數(shù)問題上存在疑惑,小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”postgresql怎么兼容MySQL if函數(shù)”的疑惑有所幫助!接下來,請跟著小編一起來學(xué)習(xí)吧!

    postgresql兼容MySQL if函數(shù)

    if函數(shù)說明

    在mysql中if()函數(shù)的用法類似于java中的三目表達(dá)式,其用處也比較多,具體語法如下:

    IF(expr1,expr2,expr3),如果expr1的值為true,則返回expr2的值,如果expr1的值為false,則返回expr3的值

    postgresql自定義if函數(shù)兼容

    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使用區(qū)別

    1. dual表

    oracle獨有的表,目的是限制sql語句結(jié)構(gòu)完整

    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類型,數(shù)字和字符自動轉(zhuǎn)換為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兼容不同數(shù)據(jù)庫

    使用if標(biāo)簽判斷_databaseId,分別適配不同的數(shù)據(jù)庫,具體代碼如下:

    <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>

    到此,關(guān)于“postgresql怎么兼容MySQL if函數(shù)”的學(xué)習(xí)就結(jié)束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學(xué)習(xí),快去試試吧!若想繼續(xù)學(xué)習(xí)更多相關(guān)知識,請繼續(xù)關(guān)注億速云網(wǎng)站,小編會繼續(xù)努力為大家?guī)砀鄬嵱玫奈恼拢?/p>

    向AI問一下細(xì)節(jié)

    免責(zé)聲明:本站發(fā)布的內(nèi)容(圖片、視頻和文字)以原創(chuàng)、轉(zhuǎn)載和分享為主,文章觀點不代表本網(wǎng)站立場,如果涉及侵權(quán)請聯(lián)系站長郵箱:is@yisu.com進(jìn)行舉報,并提供相關(guān)證據(jù),一經(jīng)查實,將立刻刪除涉嫌侵權(quán)內(nèi)容。

    AI