例如,为了记录用户mac_address并计数mac_address来恢复用户登录名的PC或笔记本,以控制用户可用的最大值(起作用):
SELECT JSON_LENGTH(auth_list->'$.mac') FROM users WHERE username='admin'
所以我尝试 步骤:01创建表格
CREATE TABLE IF NOT EXISTS `users` (
`id` int(11) NOT NULL AUTO_INCREMENT COMMENT 'id',
`username` varchar(50) COLLATE utf8_unicode_ci NOT NULL COMMENT 'username',
`auth_list` json DEFAULT NULL COMMENT 'mac address list',
PRIMARY KEY (`id`),
UNIQUE KEY `id` (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci AUTO_INCREMENT=5 ;
Step:02
INSERT INTO users ( username,auth_list )
VALUES('admin','{"mac":["b0fc361479de"]}')
步骤:03(命令没有错误,但没有插入新值)
UPDATE users SET auth_list = JSON_INSERT(auth_list , '$.mac','c84wr8492eda') WHERE username='admin'
我希望 执行sql(Step02)命令后,它将使auth_list列的值结果如
{"mac": "b0fc361479de"}
到
{"mac": ["b0fc361479de","c84wr8492eda"]}
顺便
答案 0 :(得分:0)
q1:尝试类似的操作
MySQL [localhost+ ssl/test] SQL> select json_contains(auth_list,js
r8492eda'),'$.mac') from users;
+-------------------------------------------------------------+
| json_contains(auth_list,json_quote('c84wr8492eda'),'$.mac') |
+-------------------------------------------------------------+
| 1 |
+-------------------------------------------------------------+
q2:我认为您想在步骤03中使用JSON_ARRAY_APPEND而不是JSON_INSERT。
q3:您要完全删除记录还是仅删除数组中的条目?请仔细阅读https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html