域名預(yù)訂/競(jìng)價(jià),好“米”不錯(cuò)過(guò)
這篇文章主要介紹了postgresql 查詢集合結(jié)果用逗號(hào)分隔返回字符串處理的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧。
關(guān)鍵字:
1string_agg('' , '')
例如:
1select string_agg(name||'' , ',') from sys_user
補(bǔ)充:PostgreSQL 字段用逗號(hào) “,”隔開(kāi) 判斷是否含有某個(gè)值
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']
結(jié)果:
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']
結(jié)果:
id name course
3 王五 0,4,2
SELECT * FROM "public"."T_STUDENT" WHERE string_to_array(course, ',') && ARRAY['5','8','225','111']
結(jié)果:
id name course
4 張三 1,2,5,7
2 劉一 1,5,8,9
文章來(lái)源:腳本之家
來(lái)源地址:https://www.jb51.net/article/205152.htm
申請(qǐng)創(chuàng)業(yè)報(bào)道,分享創(chuàng)業(yè)好點(diǎn)子。點(diǎn)擊此處,共同探討創(chuàng)業(yè)新機(jī)遇!