1、博客中关于 PostgreSql Array操作的操作符和函数

https://my.oschina.net/Kenyon/blog/133974

2、官方文档:

https://www.postgresql.org/docs/9.2/functions-array.html

3、pgsql中的窗口函数

https://blog.csdn.net/xfg0218/article/details/104340898

–最后一句,提示array_agg() 在9.20 .将结果集转成数组的 array_agg()

4、关于自己的sql

select dev.name as devName,rel.id,rel.plan_id,rel.dev_id,rel.spare_ids ,ARRAY_AGG(coalesce(spare.name,''))  as spareName
from rs_maintenance_plan_dev_rel rel
left join rs_dev_info dev on dev.id=rel.dev_id
left join rs_dev_spare_part_info spare on string_to_array(spare.id,',') && rel.spare_ids::text[]where rel.plan_id='832575251038732288' group by dev.name ,rel.id,rel.plan_id,rel.dev_id,rel.spare_ids

– 结尾,最后的结果不是想要的,空的效果。{},没加coalesce之前是{NULL}

上述sql在java-mybaties中出现问题了
&& 解析成了 and
### Error querying database.  Cause: org.postgresql.util.PSQLException: ERROR: argument of AND must be type boolean, not type text[]位置:296
### The error may exist in file [E:\etcom\work5\basic-web\target\classes\mapper\maintenance\RsMaintenancePlanDevRelMapper.xml]
### The error may involve cn.etcom.web.dao.resource.maintenance.RsMaintenancePlanDevRelMapper.selectList2-Inline
### The error occurred while setting parameters
### SQL: SELECT dev.name AS devName, rel.id, rel.plan_id, rel.dev_id, rel.spare_ids, ARRAY_AGG(coalesce(spare.name, '')) AS spareNames FROM rs_maintenance_plan_dev_rel rel LEFT JOIN rs_dev_info dev ON dev.id = rel.dev_id AND dev.tenant_id = '682230092146921472' LEFT JOIN rs_dev_spare_part_info spare ON string_to_array(spare.id, ',') AND rel.spare_ids::text[] AND spare.tenant_id = '682230092146921472' WHERE rel.plan_id = ? AND rel.tenant_id = '682230092146921472' GROUP BY dev.name, rel.id, rel.plan_id, rel.dev_id, rel.spare_ids
### Cause: org.postgresql.util.PSQLException: ERROR: argument of AND must be type boolean, not type text[]位置:296
; bad SQL grammar []; nested exception is org.postgresql.util.PSQLException: ERROR: argument of AND must be type boolean, not type text[]位置:296] with root cause
org.postgresql.util.PSQLException: ERROR: argument of AND must be type boolean, not type text[]位置:296at org.postgresql.core.v3.QueryExecutorImpl.receiveErrorResponse(QueryExecutorImpl.java:2505) ~[postgresql-42.2.9.jar:42.2.9]at org.postgresql.core.v3.QueryExecutorImpl.processResults(QueryExecutorImpl.java:2241) ~[postgresql-42.2.9.jar:42.2.9]at org.postgresql.core.v3.QueryExecutorImpl.execute(QueryExecutorImpl.java:310) ~[postgresql-42.2.9.jar:42.2.9]at org.postgresql.jdbc.PgStatement.executeInternal(PgStatement.java:447) ~[postgresql-42.2.9.jar:42.2.9]at org.postgresql.jdbc.PgStatement.execute(PgStatement.java:368) ~[postgresql-42.2.9.jar:42.2.9]

最后将 && 有交集就t 的改成包含于 <@
即:
‘& l t;@’ 最后mybatis中的sql:

<select id="selectList2" parameterType="java.lang.String" resultMap="LinkResultMap">select dev.name as devName,rel.id,rel.plan_id,rel.dev_id,rel.spare_ids ,ARRAY_AGG(coalesce(spare.name,''))  as spareNamesfrom rs_maintenance_plan_dev_rel relleft join rs_dev_info dev on dev.id=rel.dev_idleft join rs_dev_spare_part_info spare onstring_to_array(spare.id,',') /*<![CDATA[&& ]]>*/ &lt;@ rel.spare_ids::text[]where rel.plan_id=#{planId}group by dev.name ,rel.id,rel.plan_id,rel.dev_id,rel.spare_ids</select>

5、项目其他地方的应用,数据是数组字段的地方

  • 先说引用的mybatis-plus的版本3.3.0,数据库是pgsql,pom.xml配置如下:
<dependency><groupId>com.baomidou</groupId><artifactId>mybatis-plus-boot-starter</artifactId><version>3.3.0</version>
</dependency>
  • 实体类中的属性如下:
/*** 设备图片*/
@TableField(value = "icon",typeHandler = ArrayTypeHandler.class)
@ApiModelProperty(value = "设备图片")
private String[] icon;
  • XXmapper.xml中的resultMap中的内容如下:
<result column="icon" jdbcType="ARRAY" property="icon" typeHandler="org.apache.ibatis.type.ArrayTypeHandler" />
  • 其他使用数组当成查询条件的如下:
<select id="selectPage2" resultMap="BaseResultMap2">select<include refid="Base_Column_List2"></include>from rs_dev_info infoleft join rs_dev_type_info typeinfo on info.dev_type_id = typeinfo.idleft join rs_dev_supplier_info sup on info.supplier = sup.idleft join rs_dev_supplier_info sup2 on info.producer = sup2.idleft join rs_area_info area on info.area_id = area.idleft join sys_dept_info dept on info.dept_id = dept.idwhere 1=1<if test="rsDevInfo.name != null and rsDevInfo.name != ''"><bind name="nameLike" value="'%' + rsDevInfo.name + '%'"/>and info.name like #{nameLike,jdbcType=VARCHAR}</if><if test="rsDevInfo.code != null and rsDevInfo.code != ''"><bind name="codeLike" value="'%' + rsDevInfo.code + '%'"/>and info.code like #{codeLike,jdbcType=VARCHAR}</if><if test="rsDevInfo.devTypeId != null and rsDevInfo.devTypeId != ''"><bind name="devTypeId" value="rsDevInfo.devTypeId"/>and info.dev_type_id = #{devTypeId}</if><if test="rsDevInfo.deviceAddr != null and rsDevInfo.deviceAddr != ''"><bind name="deviceAddr" value="'%' + rsDevInfo.deviceAddr + '%'"/>and info.device_addr like #{deviceAddr,jdbcType=VARCHAR}</if><if test="rsDevInfo.areaId != null and rsDevInfo.areaId != ''"><bind name="areaId" value="rsDevInfo.areaId"/>and info.area_id = #{areaId}</if><if test="rsDevInfo.deptId != null and rsDevInfo.deptId != ''"><bind name="deptId" value="rsDevInfo.deptId"/>and info.dept_id = #{deptId}</if><if test="rsDevInfo.supplier != null and rsDevInfo.supplier != ''"><bind name="supplier" value="rsDevInfo.supplier"/>and info.supplier = #{supplier}</if><if test="rsDevInfo.runStatus != null and rsDevInfo.runStatus != ''"><bind name="runStatus" value="rsDevInfo.runStatus"/>and info.run_status = #{runStatus}</if><if test="rsDevInfo.devIds != null">and info.id not in (<foreach collection="rsDevInfo.devIds" item="devId" separator=",">#{devId}</foreach>)</if><if test="rsDevInfo.devNo != null and rsDevInfo.devNo != ''"><bind name="devNo" value="rsDevInfo.devNo"/>and info.dev_no = #{devNo}</if>and   (string_to_array(info.area_id,',') &lt;@  (select aread_id from rs_user_area_rel where user_no=#{rsDevInfo.opUser})::text[])and   (string_to_array(info.dev_type_id,',') &lt;@  (select dev_type_id from rs_user_area_rel where user_no=#{rsDevInfo.opUser})::text[])order by info.name asc</select>

string_to_array 就是讲单个字段列,转成数组。

Pgsql中数组字段当作查询条件和输出数组相关推荐

  1. Sqlserver中格式化日期作为查询条件的语句写法

    场景 查询sqlserver数据库中某一天的数据的数量. 要以一个日期字段作为查询条件. 这时就要使用格式化日期的函数进行日期的比较. 实现 成功的示例代码: SELECT COUNT( 1 ) FR ...

  2. Mysql 拼接多个字段作为查询条件查询方法

    最近工作中需要把多个字段拼接作为查询条件查询,特此记录便于日后查阅. <select id="listByProgramCodeList" resultType=" ...

  3. PostgreSQL Json字段作为查询条件的解决方案

    PostgreSQL Json字段作为查询条件的解决方案 参考文章: (1)PostgreSQL Json字段作为查询条件的解决方案 (2)https://www.cnblogs.com/weigy/ ...

  4. Laravel + MongoDB 数组字段相关查询

    我的个人博客:逐步前行STEP 1.$exist 查询 是否存在这个字段 //查询所有存在标签你字段的博客 App\Blog::where('tags','$exist',true)->get( ...

  5. Springboot项目如何设计接口中敏感字段模糊查询?

    目录 前言 场景分析 实现方案 环境配置 依赖配置 代码实现 总结 前言 在<Springboot项目如何设计接口中敏感字段的加密.解密>和<Springboot项目如何设计接口中敏 ...

  6. Springboot中对jpa动态查询条件的封装

    jpa对于固定参数的条件查询比较简单,可以在Repository中直接用参数名来查询.但是对于不固定的参数查询就比较麻烦了,官方提供的是继承JpaSpecificationExecutor,然后自己拼 ...

  7. JEECG中datagrid方法自定义查询条件

    自定义加添加查询条件的用法: CriteriaQuery cq = new CriteriaQuery(EquipmentEntity.class, dataGrid); //查询条件组装器 org. ...

  8. oracle 查询 日期 条件,orcale 把日期当作查询条件

    orcale 把日期当做查询条件 根据日期查询范围 精确到天 select * from table where to_char( time,'yyyy mm dd ' )  <=   '200 ...

  9. char un 数组printf_c语言中能不能用printf函数直接输出数组?如printf(%d,a[3][3]);

    展开全部 限制在C语言中,利用printf直接输出数组是不可以的. 但是你32313133353236313431303231363533e58685e5aeb931333337616538的做法是可 ...

最新文章

  1. MT to Death,专访 ACL Fellow刘群,一个NLPer的极致表白
  2. 【内网安全】域横向smbwmi明文或hash传递
  3. 天津鑫茂工业园区--------------三维虚拟现实系统
  4. 记录docker常用的command
  5. 2018 大湾区(深圳) .NET技术分享交流会 第一期
  6. 软件项目管理四个核心价值观
  7. 用计算机的知识服务社会,科研育人案例-董立红
  8. 树莓派4b安装windows iot_树莓派4B基于python3安装opencv4全教程
  9. Android自定义view之ViewPager指示器——1
  10. php算法求出一个数可以被分解成多少个_小学数学1—6年级必考的34个数学重难点公式,赶紧给孩子收藏!...
  11. Echarts异步获取数据不显示问题
  12. 计算机室英语单词怎么读,计算机英语单词怎么读
  13. 【BZOJ4484】【JSOI2015】最小表示(拓扑排序,bitset)
  14. 使用Dreamweaver进行一个简单的图文混排1/HTML
  15. C语言编程>第三周 ⑥ 有一个已经排好序的数组。现输入一个数,要求按原来的规律将它插入数组中。
  16. uniapp锚点定位
  17. [matlab]获取多项式的系数,表达式的系数
  18. OPTIONALLY ENCLOSED BY '`'不可删除引号的情况
  19. 人脸、指纹、虹膜、行为,你需要的人工智能生物识别AI数据集全在这里
  20. android如何监听应用进入后台,回到前台时做相应逻辑

热门文章

  1. DOS命令行高级应用
  2. java中的if语句
  3. Windows命令行通过certutil命令查看文件的MD5-SHA1-SHA256校验值
  4. 在线帮助文档转CHM方法
  5. 打造高效出差流程:SAP Press 《Concur - Travel and Expense Management with SAP》
  6. Wave Vector and Wavenumber(波矢与波数)
  7. Photoshop基础知识——第九章(路径、钢笔、矢量形状工具)
  8. 2.1.4奈氏准则、香农定理
  9. stk 12.5 odtk 7.5
  10. 改善用户体验的alert提示效果