MySQL?JSON類型數(shù)據(jù)查詢方法
更新時(shí)間:2025年04月17日 10:08:11 作者:dark-Moss
這篇文章主要介紹了MySQL?JSON類型數(shù)據(jù)查詢,本文通過實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧
1、json對(duì)象
1.1、方法
- 使用對(duì)象操作的方法進(jìn)行查詢:
字段->'$.json屬性'
- 使用函數(shù)進(jìn)行查詢:
json_extract(字段, '$.json屬性')
- 獲取JSON數(shù)組/對(duì)象長度:
JSON_LENGTH()
1.2、數(shù)據(jù)
CREATE TABLE `test` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID', `goods_sn` varchar(25) NOT NULL DEFAULT '' COMMENT '商品編碼', `desc_attr` json NOT NULL COMMENT '描述屬性', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='TEST'; INSERT INTO `test`.`test`(`id`, `goods_sn`, `desc_attr`) VALUES (1, 'A0001', '{\"tag\": [\"GRS\", \"GOTS\"], \"size\": \"M\", \"color\": \"紅色\", \"material\": \"尼龍\"}'); INSERT INTO `test`.`test`(`id`, `goods_sn`, `desc_attr`) VALUES (2, 'A0002', '{\"tag\": [\"GRS\", \"GOTS\", \"MTD\"], \"size\": \"LA\", \"color\": \"黃色\", \"material\": \"純棉\"}');
1.3、查詢
-- 查詢面料不為空的商品 select * from test where desc_attr->'$.material' is not null; select * from test where JSON_EXTRACT(desc_attr, '$.material') is not null; -- 查詢面料為純棉的商品 select * from test where desc_attr->'$.material'='純棉'; select * from test where JSON_EXTRACT(desc_attr, '$.material')='純棉'; -- 查詢標(biāo)簽數(shù)量大于2的商品 select * from test where JSON_LENGTH(desc_attr->'$.tag')>2;
2、json數(shù)組
2.1、方法
- 對(duì)象操作方式查詢:
字段->'$[*].屬性'
- 使用函數(shù)查詢:
JSON_CONTAINS(字段,JSON_OBJECT('json屬性', '內(nèi)容'))
- 獲取JSON數(shù)組/對(duì)象長度:
JSON_LENGTH()
2.2、數(shù)據(jù)
CREATE TABLE `test2` ( `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT COMMENT '自增ID', `goods_sn` varchar(25) NOT NULL DEFAULT '' COMMENT '商品編碼', `desc_attrs` json NOT NULL COMMENT '描述屬性,多個(gè)', PRIMARY KEY (`id`) ) ENGINE=InnoDB COMMENT='TEST2'; INSERT INTO `test`.`test2`(`id`, `goods_sn`, `desc_attrs`) VALUES (1, 'A0001', '[{\"tag\": [\"GRS\", \"GOTS\"], \"size\": \"M\", \"color\": \"紅色\", \"material\": \"尼龍\"}, {\"tag\": [\"GRS\", \"GOTS\", \"MTD\"], \"size\": \"LA\", \"color\": \"黃色\", \"material\": \"純棉\"}]'); INSERT INTO `test`.`test2`(`id`, `goods_sn`, `desc_attrs`) VALUES (2, 'A0002', '[{\"tag\": [\"GRS\", \"GOTS\"], \"size\": \"M\", \"color\": \"紅色\", \"material\": \"尼龍\"}, {\"tag\": [\"GRS\", \"GOTS\", \"MTD\"], \"link\": \"xxx\", \"size\": \"LA\", \"color\": \"黃色\", \"material\": \"純棉\"}]'); INSERT INTO `test`.`test2`(`id`, `goods_sn`, `desc_attrs`) VALUES (3, 'A0003', '[]');
2.3、查詢
-- 查詢描述屬性不為空的商品 select * from test2 where JSON_LENGTH(desc_attrs) > 0; -- 查詢第1項(xiàng)存在顏色屬性的商品 select * from test2 where desc_attrs->'$[0].color' is not null; -- 查詢?nèi)我忭?xiàng)存在鏈接屬性的商品 select * from test2 where desc_attrs->'$[*].link' is not null; -- 查詢?nèi)我忭?xiàng)存在鏈接等于xxx屬性的商品 select * from test2 where JSON_CONTAINS(desc_attrs,JSON_OBJECT('link', 'xxx'));
注意
-- [{"link":"xxx"}] select desc_attrs->'$[*].link' from test2 where id=2; -- 查詢結(jié)果為`["xxx"]` -- 返回每一項(xiàng)的link,所以是個(gè)數(shù)組
到此這篇關(guān)于MySQL JSON類型數(shù)據(jù)查詢的文章就介紹到這了,更多相關(guān)MySQL JSON類型數(shù)據(jù)查詢內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
三種東西永遠(yuǎn)不要放到mysql數(shù)據(jù)庫里
這篇文章主要介紹了mysql數(shù)據(jù)庫不能存儲(chǔ)的三樣?xùn)|西,需要的朋友可以參考下2014-06-06用命令創(chuàng)建MySQL數(shù)據(jù)庫(de1)的方法
下面小編就為大家?guī)硪黄妹顒?chuàng)建MySQL數(shù)據(jù)庫(de1)的方法。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2017-03-03MySQL架構(gòu)體系知識(shí)點(diǎn)總結(jié)
在本篇內(nèi)容里我們給大家整理了關(guān)于MySQL架構(gòu)體系的相關(guān)知識(shí)點(diǎn)內(nèi)容以及相關(guān)實(shí)例,需要的朋友們學(xué)習(xí)下。2019-02-02SPSS連接mysql數(shù)據(jù)庫的超詳細(xì)操作教程
小編最近在學(xué)習(xí)SPSS,在為數(shù)據(jù)庫建立連接時(shí)真的踩了很多坑,這篇文章主要給大家介紹了關(guān)于SPSS連接mysql數(shù)據(jù)庫的超詳細(xì)操作教程,文中通過圖文介紹的非常詳細(xì),需要的朋友可以參考下2023-02-02