我有一个json类型列的表,我想用现有json中的新数组元素更新一个列。
需要做的是:员工打卡时在json列中添加一个数组,员工打卡时在json列中添加另一个数组。
{"emp_sheet":[{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"},{"rulecode":"PUNCH_OUT","result":1,"applytime":"2018-04-12 13:01:39"}]}
我所做的,为员工打卡:
UPDATE table
SET rule_codes = JSON_SET(COALESCE(rule_codes, '{}'), '$.emp_sheet', '{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}')
WHERE emp_id = 1
规则代码列中的结果= {"emp_sheet": "{"rulecode":"PUNCH_IN","result":1,"applytime":"2018-04-12 04:50:39"}"}
请帮我写员工打卡的更新查询。
2条答案
按热度按时间mftmpeh81#
尝试使用
JSON_ARRAY_APPEND
而不是JSON_SET
.手册-https://dev.mysql.com/doc/refman/8.0/en/json-modification-functions.html
我想可能是这样
或
zbq4xfa02#
这将是最容易的,如果你做的
$.emp_sheet
打孔时的json数组:然后在打孔时,可以向数组中添加另一个元素:
输出:
注意,当您进行设置时,输入json(
'{"rulecode ... }'
)被视为字符串,因此"
在上面的输出中。你可以用JSON_UNQUOTE
当你提取即。或者使用快捷符号
输出: