PostgreSQL中如何模拟MySQL的IF函数?
作者:小确幸
时间:2023-05-07
浏览:3
postgresql兼容MySQLif函数if函数说明在mysql中if()函数的用法类似于java中的三目表达式,其用处也比较多,具体语法如下:IF(expr1,expr2,expr3),如果expr1的值为true,则返回expr2的值,如果expr1的值为false,则返回expr3的值postgresql自定义if函数兼容createorreplacefunctionif(blnboolean,inValue1anyelement,inValue2anyelement)returnsanyelem
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 $$ 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语句结构完整
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 into table_name ( ) values ( ) insert all into table_name ( select * from dual) values ( ) id,name,age,gender #{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR}, #{item.age,jdbcType=INTEGER},#{item.gender,jdbcType=INTEGER}
作者最新文章
bartender卸载清理工具彻底删除安装残留教程
2026-10-09 15:06
Mac电脑怎么安装XMind及解决权限拦截问题
2026-10-04 13:50
WinRAR不重新安装怎么把英文界面改成中文
2026-10-04 08:55
Photoshop抠图教程详细步骤图解:新手入门常用方法与技巧
2026-09-22 14:38
Python安装后怎么打开:使用IDLE或命令行启动解释器
2026-09-16 13:54
上一篇:
Python多进程的应用场景和使用方法
下一篇:
Python中迭代器与生成器的使用方法
热门文章
更多
精品专题
更多
Mac软件
更多
WINDOWS
更多










































