我如何 select 具有这种结构的 json 列 Mysql 5.7
How do I select a json column with such a structure for Mysql 5.7
我的 mysql5.7 数据库 table 中有一个产品 table 具有以下属性 Products
- id : 整数
- 属性:Json类型
- 价格
我插入了以下记录:
INSERT INTO `products` (`id`, `attributes`, `name`, `cost_price`,
`selling_price`) VALUES (NULL, '{\"size\": [{\"value\": \"M\"},
{\"value\": \"L\"}], \"test\": 1, \"color\": [\"black\", \"maroon\",
\"orange\"]}', 'P2', '10', '20');
这是列的 json 值:
{"size": [{"value": "M"}, {"value": "L"}], "test": 1, "color":
["black", "maroon", "orange"]}
如何获取具有 size:L 的数据记录?如何在 Mysql 中执行 sql 查询?我试过这个但它不起作用:
SELECT * FROM `products` where attributes->>'$.size[*].value'='M'
希望有人能帮助我理解,因为我是 mysql 中 Json 结构的新手。提前致谢
我设法解决了它,我相信这是一个可能的查询:
select * from products where json_contains(attributes, '{"size":{"value" : "XL"}}');
我的 mysql5.7 数据库 table 中有一个产品 table 具有以下属性 Products
- id : 整数
- 属性:Json类型
- 价格
我插入了以下记录:
INSERT INTO `products` (`id`, `attributes`, `name`, `cost_price`,
`selling_price`) VALUES (NULL, '{\"size\": [{\"value\": \"M\"},
{\"value\": \"L\"}], \"test\": 1, \"color\": [\"black\", \"maroon\",
\"orange\"]}', 'P2', '10', '20');
这是列的 json 值:
{"size": [{"value": "M"}, {"value": "L"}], "test": 1, "color": ["black", "maroon", "orange"]}
如何获取具有 size:L 的数据记录?如何在 Mysql 中执行 sql 查询?我试过这个但它不起作用:
SELECT * FROM `products` where attributes->>'$.size[*].value'='M'
希望有人能帮助我理解,因为我是 mysql 中 Json 结构的新手。提前致谢
我设法解决了它,我相信这是一个可能的查询:
select * from products where json_contains(attributes, '{"size":{"value" : "XL"}}');