这篇文章主要介绍了postgresql 查询集合结果用逗号分隔返回字符串处理的操作,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧。
关键字:
1string_agg(” , ”)
例如:
1select 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
文章来源:脚本之家
来源地址:https://www.jb51.net/article/205152.htm
版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至253000106@qq.com举报,一经查实,本站将立刻删除。
发布者:SEO优化专员,转转请注明出处:https://www.chuangxiangniao.com/p/893269.html