postgresql 查询集合结果用逗号分隔返回字符串处理的操作

时间:2021-05-24

关键字:

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 course4 张三 1,2,5,71 李四 12,45,1,5,66,7,892 刘一 1,5,8,93 王五 0,4,2SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') @> ARRAY['2','7']结果:id name course4 张三 1,2,5,7SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') <@ array['5','12','45','1','0','4','2']结果:id name course3 王五 0,4,2SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') && ARRAY['5','8','225','111']结果:id name course4 张三 1,2,5,72 刘一 1,5,8,9

以上为个人经验,希望能给大家一个参考,也希望大家多多支持。如有错误或未考虑完全的地方,望不吝赐教。

声明:本页内容来源网络,仅供用户参考;我单位不保证亦不表示资料全面及准确无误,也不保证亦不表示这些资料为最新信息,如因任何原因,本网内容或者用户因倚赖本网内容造成任何损失或损害,我单位将不会负任何法律责任。如涉及版权问题,请提交至online#300.cn邮箱联系删除。

相关文章