我有这个mySql(5.7.14版)字段。
id shop_name json_string
1 shop_1 [{"your_number": "2", "player_id": "6789" }, {"your_number": "3", "player_id": "9877" }, {"your_number": "4", "player_id": "132456" }]
2 shop_2 [{"your_number": "2", "player_id": "6789" }, {"your_number": "3", "player_id": "9877" }, {"your_number": "4", "player_id": "132456" }]
如何根据ID和JSON your_number更新字符串?
例如,我要删除your_number = 3个字符串,其中id = 2
结果:
id shop_name json_string
2 shop_2 [{"your_number": "2", "player_id": "6789" }, {"your_number": "4", "player_id": "132456" }]
谢谢!
答案 0 :(得分:1)
我对此进行了测试,并且可以正常工作
<div id="container"></div>
我必须评论说,这并不会使JSON对于您的应用程序来说似乎是个好主意。
如果您需要处理JSON文档,则将数据库存储为标准化的表集会更容易。
UPDATE Shops
SET json_string = JSON_REMOVE(
json_string,
SUBSTRING_INDEX(
JSON_UNQUOTE(JSON_SEARCH(json_string, 'one', '3', null, '$[*].your_number')), '.', 1)
)
WHERE id = 2;
现在,您可以使用更简单的SQL删除播放器:
CREATE TABLE Shops (
id INT PRIMARY KEY,
shop_name VARCHAR(10)
);
CREATE TABLE ShopPlayers (
shop_id INT NOT NULL,
your_number INT NOT NULL,
player_id INT NOT NULL,
PRIMARY KEY (shop_id, your_number)
);
一段时间以来,我一直在关注有关Stack Overflow上mysql和json的问题,我不得不说,在我见过的几乎所有情况下,如果以传统方式设计表会更好。归一化规则。 SQL查询将更易于编写和调试,它们将运行得更快,并且数据库将更有效地存储数据。