您现在的位置是:网站首页> 编程资料编程资料
postgresql 查询集合结果用逗号分隔返回字符串处理的操作_PostgreSQL_
2023-12-09
219人已围观
简介 postgresql 查询集合结果用逗号分隔返回字符串处理的操作_PostgreSQL_
关键字:
string_agg('' , '')
例如:
select string_agg(name||'' , ',') from sys_user
补充:PostgreSQL 字段用逗号 “,”隔开 判断是否含有某个值
Array Functions and Operators
https://www.postgresql.org/docs/9.2/functions-array.html
-- ---------------------------- -- Table structure for T_STUDENT -- ---------------------------- DROP TABLE IF EXISTS "public"."T_STUDENT"; CREATE TABLE "public"."T_STUDENT" ( "id" int4, "name" varchar(255) COLLATE "default", "course" varchar(255) COLLATE "default" ) WITH (OIDS=FALSE) ; -- ---------------------------- -- Records of T_STUDENT -- ---------------------------- INSERT INTO "public"."T_STUDENT" VALUES ('1', '李四', '12,45,1,66,7,89'); INSERT INTO "public"."T_STUDENT" VALUES ('2', '刘一', '1,5,8,9'); INSERT INTO "public"."T_STUDENT" VALUES ('3', '王五', '0,4,2'); INSERT INTO "public"."T_STUDENT" VALUES ('4', '张三', '1,2,5,7'); -- ---------------------------- -- Alter Sequences Owned By -- ---------------------------- -- ---------------------------- -- Table structure for T_STUDENT -- ---------------------------- DROP TABLE IF EXISTS "public"."T_STUDENT"; CREATE TABLE "public"."T_STUDENT" ( "id" int4, "name" varchar(255) COLLATE "default", "course" varchar(255) COLLATE "default" ) WITH (OIDS=FALSE) ; -- ---------------------------- -- Records of T_STUDENT -- ---------------------------- INSERT INTO "public"."T_STUDENT" VALUES ('1', '李四', '12,45,1,66,7,89'); INSERT INTO "public"."T_STUDENT" VALUES ('2', '刘一', '1,5,8,9'); INSERT INTO "public"."T_STUDENT" VALUES ('3', '王五', '0,4,2'); INSERT INTO "public"."T_STUDENT" VALUES ('4', '张三', '1,2,5,7'); -- ---------------------------- -- Alter Sequences Owned By -- ---------------------------- id name course 4 张三 1,2,5,7 1 李四 12,45,1,5,66,7,89 2 刘一 1,5,8,9 3 王五 0,4,2 SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') @> ARRAY['2','7'] 结果: id name course 4 张三 1,2,5,7 SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') <@ array['5','12','45','1','0','4','2'] 结果: id name course 3 王五 0,4,2 SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') && ARRAY['5','8','225','111'] 结果: id name course 4 张三 1,2,5,7 2 刘一 1,5,8,9
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。
您可能感兴趣的文章:
相关内容
- postgresql 将逗号分隔的字符串转为多行的实例_PostgreSQL_
- Redis keys命令的具体使用_Redis_
- Redis如何使用HyperLogLog的实现_Redis_
- 神魔大陆血魔职业介绍 血魔职业选择技巧_手机游戏_游戏攻略_
- 雷霆战机登陆送10钻石追击BOSS送50钻石怎么领在哪领_手机游戏_游戏攻略_
- 刀塔传奇实用购物心得分享_手机游戏_游戏攻略_
- 天天酷跑炫飞模式阵容怎么搭配?天天酷跑炫飞模式最佳阵容搭配技巧_手机游戏_游戏攻略_
- 雷霆战机6月27周末活动公告_手机游戏_游戏攻略_
- 刀塔传奇黑市老大开启的时间及条件有哪些_手机游戏_游戏攻略_
- 天天酷跑总战力提升方法介绍 天天酷跑炫飞模式总战力怎么提升?_手机游戏_游戏攻略_