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$$beginif 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$$beginif 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$$beginif 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=='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>
以上就是postgresql怎么兼容mysql if函数的详细内容。