MySQL?JSON類型數(shù)據(jù)查詢方法
更新時間:2025年04月17日 10:08:11 作者:dark-Moss
這篇文章主要介紹了MySQL?JSON類型數(shù)據(jù)查詢,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友參考下吧
1、json對象
1.1、方法
- 使用對象操作的方法進行查詢:
字段->'$.json屬性'
- 使用函數(shù)進行查詢:
json_extract(字段, '$.json屬性')
- 獲取JSON數(shù)組/對象長度:
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')='純棉'; -- 查詢標簽數(shù)量大于2的商品 select * from test where JSON_LENGTH(desc_attr->'$.tag')>2;
2、json數(shù)組
2.1、方法
- 對象操作方式查詢:
字段->'$[*].屬性'
- 使用函數(shù)查詢:
JSON_CONTAINS(字段,JSON_OBJECT('json屬性', '內(nèi)容'))
- 獲取JSON數(shù)組/對象長度:
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 '描述屬性,多個', 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項存在顏色屬性的商品 select * from test2 where desc_attrs->'$[0].color' is not null; -- 查詢?nèi)我忭棿嬖阪溄訉傩缘纳唐? select * from test2 where desc_attrs->'$[*].link' is not null; -- 查詢?nèi)我忭棿嬖阪溄拥扔趚xx屬性的商品 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"]` -- 返回每一項的link,所以是個數(shù)組
到此這篇關(guān)于MySQL JSON類型數(shù)據(jù)查詢的文章就介紹到這了,更多相關(guān)MySQL JSON類型數(shù)據(jù)查詢內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
用命令創(chuàng)建MySQL數(shù)據(jù)庫(de1)的方法
下面小編就為大家?guī)硪黄妹顒?chuàng)建MySQL數(shù)據(jù)庫(de1)的方法。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2017-03-03SPSS連接mysql數(shù)據(jù)庫的超詳細操作教程
小編最近在學習SPSS,在為數(shù)據(jù)庫建立連接時真的踩了很多坑,這篇文章主要給大家介紹了關(guān)于SPSS連接mysql數(shù)據(jù)庫的超詳細操作教程,文中通過圖文介紹的非常詳細,需要的朋友可以參考下2023-02-02