最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

postgresql如何兼容MySQL if函數(shù)

 更新時(shí)間:2023年03月20日 16:12:25   作者:Ruby-PGer  
這篇文章主要介紹了postgresql如何兼容MySQL if函數(shù)問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教

postgresql兼容MySQL if函數(shù)

if函數(shù)說(shuō)明

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

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獨(dú)有的表,目的是限制sql語(yǔ)句結(jié)構(gòu)完整

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

mysql和pgsql沒(méi)有這張表,可以直接去掉

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

2. 布爾類型

oracle和mysql沒(méi)有boolean類型,可使用number(int)或char代替

pgsql中有bool類型,數(shù)字和字符自動(dòng)轉(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 僅支持單引號(hào)

select * from table_name where name = 'abc'

mysql 單引號(hào)/雙引號(hào)都支持

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ù)庫(kù)

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

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

總結(jié)

以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。

相關(guān)文章

  • PostgreSQL常用優(yōu)化技巧示例介紹

    PostgreSQL常用優(yōu)化技巧示例介紹

    PostgreSQL的SQL優(yōu)化技巧其實(shí)和大多數(shù)使用CBO優(yōu)化器的數(shù)據(jù)庫(kù)類似,因此一些常用的SQL優(yōu)化改寫技巧在PostgreSQL也是能夠使用的。當(dāng)然也會(huì)有一些不同的地方,今天我們來(lái)看看一些在PostgreSQL常用的SQL優(yōu)化改寫技巧
    2022-09-09
  • PostgreSQL 實(shí)現(xiàn)定時(shí)job執(zhí)行(pgAgent)

    PostgreSQL 實(shí)現(xiàn)定時(shí)job執(zhí)行(pgAgent)

    這篇文章主要介紹了PostgreSQL 實(shí)現(xiàn)定時(shí)job執(zhí)行(pgAgent),具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • Postgresql數(shù)據(jù)庫(kù)SQL字段拼接方法

    Postgresql數(shù)據(jù)庫(kù)SQL字段拼接方法

    Postgresql里面內(nèi)置了很多的實(shí)用函數(shù),下面這篇文章主要給大家介紹了關(guān)于Postgresql數(shù)據(jù)庫(kù)SQL字段拼接方法的相關(guān)資料,文中通過(guò)代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2023-11-11
  • PostgreSQL的dblink擴(kuò)展模塊使用

    PostgreSQL的dblink擴(kuò)展模塊使用

    PostgreSQL dblink擴(kuò)展支持跨庫(kù)查詢,需定義返回列名和類型,可以實(shí)現(xiàn)在不同的數(shù)據(jù)庫(kù)之間進(jìn)行通信和交互,下面就一起來(lái)了解一下
    2025-06-06
  • postgresql 獲取兩個(gè)時(shí)間類型小時(shí)差值案例

    postgresql 獲取兩個(gè)時(shí)間類型小時(shí)差值案例

    這篇文章主要介紹了postgresql 獲取兩個(gè)時(shí)間類型小時(shí)差值案例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2020-12-12
  • PostgreSQL數(shù)據(jù)庫(kù)事務(wù)插入刪除及更新操作示例

    PostgreSQL數(shù)據(jù)庫(kù)事務(wù)插入刪除及更新操作示例

    這篇文章主要為大家介紹了PostgreSQL事務(wù)的插入刪除及更新操作示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪
    2022-04-04
  • 如何獲取PostgreSQL數(shù)據(jù)庫(kù)中的JSON值

    如何獲取PostgreSQL數(shù)據(jù)庫(kù)中的JSON值

    這篇文章主要介紹了如何獲取PostgreSQL數(shù)據(jù)庫(kù)中的JSON值操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧
    2021-01-01
  • PostgreSQL教程(十二):角色和權(quán)限管理介紹

    PostgreSQL教程(十二):角色和權(quán)限管理介紹

    這篇文章主要介紹了PostgreSQL教程(十二):角色和權(quán)限管理介紹,本文講解了數(shù)據(jù)庫(kù)角色、角色屬性、權(quán)限、角色成員,需要的朋友可以參考下
    2015-05-05
  • PostgreSQL中的日期/時(shí)間函數(shù)詳解

    PostgreSQL中的日期/時(shí)間函數(shù)詳解

    這篇文章主要給大家介紹了關(guān)于PostgreSQL中日期/時(shí)間函數(shù)的相關(guān)資料,文章通過(guò)實(shí)例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下
    2022-01-01
  • PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例

    PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例

    這篇文章主要介紹了PostgreSQL中調(diào)用存儲(chǔ)過(guò)程并返回?cái)?shù)據(jù)集實(shí)例,本文給出一創(chuàng)建數(shù)據(jù)表、插入測(cè)試數(shù)據(jù)、創(chuàng)建存儲(chǔ)過(guò)程、調(diào)用創(chuàng)建存儲(chǔ)過(guò)程和運(yùn)行效果完整例子,需要的朋友可以參考下
    2015-01-01

最新評(píng)論

仁怀市| 赤壁市| 靖州| 晋州市| 汝城县| 西贡区| 铁岭县| 安阳市| 正定县| 岫岩| 长葛市| 内黄县| 临汾市| 霍城县| 综艺| 云浮市| 曲麻莱县| 通州市| 隆尧县| 金川县| 虎林市| 玉门市| 乐昌市| 岑溪市| 福州市| 达日县| 新竹市| 大丰市| 民乐县| 台东市| 襄樊市| 东阳市| 华坪县| 博爱县| 专栏| 钟祥市| 金堂县| 塔城市| 巢湖市| 南澳县| 临高县|