如何在MySQL查询本身中检索存储在JSON数组中的值?

UI *_*Dev 3 mysql sql json

我有下表

product_id    product_name    image_path                             misc
----------   --------------  ------------                           ------
     1            flex        http://firstpl...      {"course_level_id":19,"group_id":"40067"}
     2           Android      http://firstpl...      {"course_level_id":20,"group_id":"40072"}
Run Code Online (Sandbox Code Playgroud)

那么如何从"misc"列中检索product_name,image_path和"group_id"值,如"40067".

我尝试了下面的查询,但它在Misc列中返回1/0.

SELECT product_name,image_path,misc REGEXP '(.*\"group_id\":*)' as Misc FROM ref_products where product_id=1
Run Code Online (Sandbox Code Playgroud)

任何想法的人怎么做?

Tur*_*ile 6

REGEXP函数只返回0或1.您将不得不使用其他字符串函数.

试试这个:substr(misc,locate('group_id',misc)+11,5) as Misc.但是假设group_id总是有5个字符.

所以这更好:substring_index(substr(misc,locate('group_id',misc)+char_length('group_id')+3),'"',1) as Misc.

这是一个显示它工作的小提琴:http://sqlfiddle.com/#!2/ea02e/15

编辑您可以+3通过在字符串中包含双引号和冒号来摆脱幻数,如下所示: substring_index(substr(misc,locate('"group_id":"',misc)+char_length('"group_id":"')),'"',1) as Misc