经验首页 前端设计 程序设计 Java相关 移动开发 数据库/运维 软件/图像 大数据/云计算 其他经验
当前位置:技术经验 » 数据库/运维 » PostgreSQL » 查看文章
postgresql如何兼容MySQL if函数
来源:jb51  时间:2023/3/22 9:23:18  对本文有异议

postgresql兼容MySQL if函数

if函数说明

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

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

postgresql自定义if函数兼容

  1. create or replace function if(bln boolean,inValue1 anyelement,inValue2 anyelement)
  2. returns anyelement as
  3. $$
  4. begin
  5. if bln=true then
  6. return inValue1;
  7. else
  8. return inValue2;
  9. end if;
  10. end;
  11. $$
  12. language plpgsql;
  13.  
  14. create or replace function if(bln boolean,inValue1 numeric,inValue2 numeric)
  15. returns numeric as
  16. $$
  17. begin
  18. if bln=true then
  19. return inValue1;
  20. else
  21. return inValue2;
  22. end if;
  23. end;
  24. $$
  25. language plpgsql;
  26.  
  27. create or replace function if(bln boolean,inValue1 numeric,inValue2 text)
  28. returns text as
  29. $$
  30. begin
  31. if bln=true then
  32. return inValue1;
  33. else
  34. return inValue2;
  35. end if;
  36. end;
  37. $$
  38. 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. <insert id="insertBatch" parameterType="java.util.List">
  2. ? ? <if test="_databaseId=='mysql' or _databaseId=='postgresql'">
  3. ? ? ? ? insert into table_name?
  4. ? ? ? ? (<include refid="insertBatchColumn"></include>)
  5. ? ? ? ? values
  6. ? ? ? ? <foreach collection="list" item="item" index="index" separator="," >
  7. ? ? ? ? ? ? (<include refid="insertBatchValue"></include>)
  8. ? ? ? ? </foreach>
  9. ? ? </if>
  10. ? ? <if test="_databaseId=='oracle'">
  11. ? ? ? ? insert all
  12. ? ? ? ? <foreach collection="list" item="item" index="index" separator="">
  13. ? ? ? ? ? ? into table_name?
  14. ? ? ? ? ? ? (<include refid="insertBatchColumn"></include>)
  15. ? ? ? ? ? ? values (<include refid="insertBatchValue"></include>)
  16. ? ? ? ? </foreach>
  17. ? ? ? ? select * from dual
  18. ? ? </if>
  19. </insert>
  20. ?
  21. <sql id="insertBatchColumn">
  22. ? ? id,name,age,gender
  23. </sql>
  24. <sql id="insertBatchValue">
  25. ? ? #{item.id,jdbcType=VARCHAR}, #{item.name,jdbcType=VARCHAR},?
  26. ? ? #{item.age,jdbcType=INTEGER},#{item.gender,jdbcType=INTEGER}
  27. </sql>

总结

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

 友情链接:直通硅谷  点职佳  北美留学生论坛

本站QQ群:前端 618073944 | Java 606181507 | Python 626812652 | C/C++ 612253063 | 微信 634508462 | 苹果 692586424 | C#/.net 182808419 | PHP 305140648 | 运维 608723728

W3xue 的所有内容仅供测试,对任何法律问题及风险不承担任何责任。通过使用本站内容随之而来的风险与本站无关。
关于我们  |  意见建议  |  捐助我们  |  报错有奖  |  广告合作、友情链接(目前9元/月)请联系QQ:27243702 沸活量
皖ICP备17017327号-2 皖公网安备34020702000426号