postgresqlはMySQLのif関数とどのように互換性がありますか

PHPz
リリース: 2023-06-01 19:52:12
転載
2607 人が閲覧しました

    postgresql は MySQL if 関数と互換性があります

    if 関数の説明

    mysql での if() 関数の使用法は次のようになります。 Java の 3 つ 目的式には多くの用途があります。具体的な構文は次のとおりです:

    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 . デュアル テーブル

    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にはブール型がないため、代わりにnumber (int)またはcharを使用できます

    pgsql は bool 型で、数値と文字は自動的に boolean 型 (0→f、1→t、no→f、yes→t)に変換されます

    3. テーブルのエイリアスを更新します

    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 中国語 Web サイトの他の関連記事を参照してください。

    関連ラベル:
    ソース:yisu.com
    このウェブサイトの声明
    この記事の内容はネチズンが自主的に寄稿したものであり、著作権は原著者に帰属します。このサイトは、それに相当する法的責任を負いません。盗作または侵害の疑いのあるコンテンツを見つけた場合は、admin@php.cn までご連絡ください。
    最新の問題
    人気のチュートリアル
    詳細>
    最新のダウンロード
    詳細>
    ウェブエフェクト
    公式サイト
    サイト素材
    フロントエンドテンプレート
    私たちについて 免責事項 Sitemap
    PHP中国語ウェブサイト:福祉オンライン PHP トレーニング,PHP 学習者の迅速な成長を支援します!