讀前思考:
你沒想到解決辦法?PostgreSQL 數據庫本身就支持還是另有解決辦法?
使用JOSNB作為字段類型。現在開始,,,,,,,,,,,,,,,,,,
json如下:
"rule":{ "tags": { "target": "logon" }, "time": "2019-11-15 10:00:00", "method": "scheduled", "source": "backend", "aliases": [], "sendType": "tag", "registrationIds": [] }
現在我想獲取 rule 字段 里面的 sendType="tag",那現在怎么搞?
代碼如下:
#####Mybatis##### <select id="selectPushDataList" resultMap="BaseResultMap" parameterType="com.jpc.JpushData" > SELECT <include refid="Base_Column_List" /> FROM jp_push jpt <where> delete_flag=1 and (rule::json->>'sendType')::text = 'tag' <if test="name != null and name !=''" > and name =#{name,jdbcType=VARCHAR} </if> ORDER BY create_time ASC LIMIT ${pageSize} OFFSET ${(currentPage - 1) * pageSize} </where> </select> ####PostgreSQL SQL#### SELECT uuid,create_user,create_time,update_user,update_time,delete_flag,NAME,type,push,rule, STATUS FROM jp_push jpt WHERE jpt.delete_flag = 1 AND ( jpct.rule :: json ->> 'sendType' ) :: text = 'tag' AND jpt.NAME = 'testname' ORDER BY jpt.create_time ASC LIMIT 20 OFFSET 0
結果如下:
再看如下的json串:
--如何獲取title,description呢--------
----------pre- zh en app----------
"pre": {
"zh": {
"app": {
"title": "test",
"description": "test",
"images": [
"https://www.baidu.com/",
"https://www.baidu.com/1"
]
}
},
"en": {
"app": {
"title": "test",
"description": "test",
"images": [
"https://www.baidu.com/",
"https://www.baidu.com/1"
]
}
}
---------------pre zh en web------------------------------------
"pre": {
"zh": {
"web": {
"title": "test",
"description": "test",
"images": [
"https://www.baidu.com/",
"https://www.baidu.com/1"
]
}
},
"en": {
"web": {
"title": "test",
"description": "test",
"images": [
"https://www.baidu.com/",
"https://www.baidu.com/1"
]
}
}
}
如何寫sql? 把語言和端作為一個變量傳進去...
SELECT uuid, address,pre, pre :: json -> '${lunguage}' -> '${device}' ->> 'title' AS title, pre :: json -> '${lunguage}' -> '${device}' ->> 'description' AS description FROM "ban".test j
獲取 zh-web不為空的內容:
lunguage 傳 zh,device傳 web
SELECT uuid,address, pre, pre :: json -> '${lunguage}' -> '${device}' ->> 'title' AS title, pre :: json -> '${lunguage}' -> '${device}' ->> 'description' AS description FROM "ban".test j where pre :: json -> '${lunguage}' -> '${device}' is not null
獲取 zh-web-images數組里面的第一個圖片信息不為空的內容:
SELECT
uuid,address, pre, pre :: json -> '${lunguage}' -> '${device}' ->> 'title' AS title, pre :: json -> '${lunguage}' -> '${device}' ->> 'description' AS description, pre :: json -> '${lunguage}' -> '${device}' ->>0::'images' AS images FROM
"ban".test j where pre :: json -> '${lunguage}' -> '${device}' is not null
總結:PostgreSQL 本身就支持以json作為sql的查詢條件。