服务器之家:专注于VPS、云服务器配置技术及软件下载分享
分类导航

Mysql|Sql Server|Oracle|Redis|MongoDB|PostgreSQL|Sqlite|DB2|mariadb|Access|数据库技术|

服务器之家 - 数据库 - PostgreSQL - postgresql如何兼容MySQL if函数

postgresql如何兼容MySQL if函数

2023-03-21 15:05Ruby-PGer PostgreSQL

这篇文章主要介绍了postgresql如何兼容MySQL if函数问题,具有很好的参考价值,希望对大家有所帮助。如有错误或未考虑完全的地方,望不吝赐教

postgresql兼容MySQL if函数

if函数说明

在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:

IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值

postgresql自定义if函数兼容

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
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语句结构完整

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

mysql和pgsql没有这张表,可以直接去掉

?
1
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支持

?
1
update table_name t set t.name = 'abc' where id = 1

4. 字符串传值

pgsql 、oracle 仅支持单引号

?
1
select * from table_name where name = 'abc'

mysql 单引号/双引号都支持

?
1
select * from table_name where name = "abc"

5. 批量插入

mysql、pgsql批量插入

?
1
insert into table_name() values()

oracle批量插入

?
1
insert all into table_name() values()

mybatis兼容不同数据库

使用if标签判断_databaseId,分别适配不同的数据库,具体代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
<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>

总结

以上为个人经验,希望能给大家一个参考,也希望大家多多支持服务器之家。

原文链接:https://blog.csdn.net/qq_32935175/article/details/111642194

延伸 · 阅读

精彩推荐