postgresql如何兼容MySQL if函數(shù)
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獨(dú)有的表,目的是限制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ù)字和字符自動(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 僅支持單引號
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>總結(jié)
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
PostgreSQL誤刪數(shù)據(jù)庫該怎么辦詳解
這篇文章主要介紹了PostgreSQL中誤刪數(shù)據(jù)庫的恢復(fù)方法,包括備份恢復(fù)、歸檔日志恢復(fù)和操作系統(tǒng)層面的快照恢復(fù),文中通過代碼介紹的非常詳細(xì),需要的朋友可以參考下2025-03-03
PostgreSQL數(shù)據(jù)庫時(shí)間類型相加減操作
PostgreSQL提供了許多函數(shù),這些函數(shù)返回與當(dāng)前日期和時(shí)間相關(guān)的值,下面這篇文章主要給大家介紹了關(guān)于PostgreSQL數(shù)據(jù)庫時(shí)間類型相加減操作的相關(guān)資料,需要的朋友可以參考下2023-10-10
PostgreSQL數(shù)據(jù)庫視圖及子查詢使用操作
這篇文章主要為大家介紹了PostgreSQL數(shù)據(jù)庫視圖及子查詢的使用操作,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步早日升職加薪2022-04-04
Mac系統(tǒng)重置PostgreSQL密碼的方法示例代碼
PostgreSQL 是一個(gè)免費(fèi)的對象-關(guān)系數(shù)據(jù)庫服務(wù)器(ORDBMS),在靈活的BSD許可證下發(fā)行。這篇文章主要介紹了Mac系統(tǒng)重置PostgreSQL密碼的方法示例代碼,需要的朋友可以參考下2020-02-02
解決postgresql 數(shù)字轉(zhuǎn)換成字符串前面會(huì)多出一個(gè)空格的問題
這篇文章主要介紹了解決postgresql 數(shù)字轉(zhuǎn)換成字符串前面會(huì)多出一個(gè)空格的問題,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2020-12-12
詳解PostgreSql數(shù)據(jù)庫對象信息及應(yīng)用
這篇文章主要介紹了PostgreSql數(shù)據(jù)庫對象信息及應(yīng)用,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-12-12
PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng)
這篇文章主要介紹了PostgreSQL 如何獲取當(dāng)前日期時(shí)間及注意事項(xiàng),文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2019-10-10
PostgreSQL 主備數(shù)據(jù)宕機(jī)恢復(fù)測試方案
這篇文章主要介紹了PostgreSQL 主備數(shù)據(jù)宕機(jī)恢復(fù)測試方案,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2021-01-01

