如何从对象的mysql json数组中获取特定的项目记录

时间:2018-12-28 09:47:34

标签: mysql sql json

尝试以下查询:

select
  `prodname`,
  parent_cat_id,
  sub_cat_id,
  color,
  JSON_EXTRACT(`product_items`, '$[*].quantity') AS 'qty',
  JSON_EXTRACT(`product_items`, '$[*].offerprice') AS 'price'
from
  product_details
where
  prodid=11 AND
  JSON_CONTAINS(product_items, '{"size": "S"}');

在列表中提供数组,以下是我的表结构:

CREATE TABLE `product_details` (
  `prodid` int(11) AUTO_INCREMENT NOT NULL,
  `prodname` varchar(255) UNIQUE NOT NULL,
  `color` varchar(25) NOT NULL,
  `product_items` JSON,
  `attributelist` JSON,
   PRIMARY KEY(prodid)
) ENGINE=MyISAM DEFAULT CHARSET=utf8;

1 个答案:

答案 0 :(得分:0)

Set Json Data in Dictionary and then Append this "Data Dictionary" in Array of type Dictionary.Like 
DataDictionary["Column name"] = "Value"
DataArray.Append(DataDictionary)
Now You can get Any item.like
DataArray[indexnum]."Select item that you want by giving Key"