首頁  >  文章  >  資料庫  >  postgresql怎麼相容MySQL if函數

postgresql怎麼相容MySQL if函數

PHPz
PHPz轉載
2023-06-01 19:52:122609瀏覽

    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==&#39;mysql&#39; or _databaseId==&#39;postgresql&#39;">
            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==&#39;oracle&#39;">
            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函數的詳細內容。更多資訊請關注PHP中文網其他相關文章!

    陳述:
    本文轉載於:yisu.com。如有侵權,請聯絡admin@php.cn刪除