1. postgresql 某字段用逗号分隔,查询某个值是否在其中
比如有个字段值是 1,2,3
查看1是否在里面
select id, gate_type, gate_name from yg_gate_base_b where '1' = ANY ( STRING_TO_ARRAY( gate_type, ',' ) ) order by gate_type desc
2. postgresql 某字段用逗号分隔,关联表查询
比如一个表,字段为 1,2,
另有一张字典表,1:节制闸,2:排水闸
需要查询结果为:节制闸,排水闸
SELECT a.id, a.gate_type, string_agg ( s.name, ',' ) FROM yg_gate_base_b a LEFT JOIN sys_dict_list s ON s.code = ANY ( STRING_TO_ARRAY( a.gate_type, ',' ) ) where s.dic_name = '水闸类型' and '1' = ANY ( STRING_TO_ARRAY( a.gate_type, ',' ) ) GROUP BY a.id, a.gate_type ORDER BY a.id ASC
查询效果