我正在尝试插入/更新字符串 (JSON)。插入/更新值时出现数据截断错误。
我尝试创建 JSON 类型列并传递 JSON_OBJECT() 类型,但也失败了。
select '''[{"id":"1202","title":"Asian","notes":"","active":"1"}]''';
CREATE TABLE mktesttable (
id int NOT NULL,
s VARCHAR(34530) NOT NULL
);
INSERT INTO mktesttable
select 1, '''[{"id":"1202","title":"Asian","notes":"","active":"1"}]''';
select * from mktesttable;
//有效
<小时/>INSERT INTO mktesttable
SELECT
patient_data.id,
CONCAT(
'''[{"id":"', patient_data.race,
'","title":"', list_options.title,
'","notes":"', list_options.notes,
'","active":"', list_options.active,
'"}]'''
) as s
FROM
patient_data
INNER JOIN list_options
ON patient_data.race = list_options.id order by 1 desc
产生相同的结果(ID 和数据不同)但不起作用
最佳答案
如果要存储 JSON 对象,应使用 JSON
数据类型而不是字符串。要创建 JSON 对象,您可以使用 JSON_OBJECT
。
CREATE TABLE mktesttable (
id int NOT NULL,
s JSON NOT NULL
);
INSERT INTO mktesttable
SELECT
patient_data.id,
JSON_OBJECT(
'id', patient_data.race,
'title', list_options.title,
'notes', list_options.notes,
'active', list_options.active,
)
FROM
patient_data
INNER JOIN list_options
ON patient_data.race = list_options.id
ORDER BY patient_data.id desc
如果您需要示例数据中所示的 JSON 数组,则:
JSON_ARRAY(
JSON_OBJECT(
'id', patient_data.race,
'title', list_options.title,
'notes', list_options.notes,
'active', list_options.active,
)
)
关于mysql - 数据截断: '' While Inserting or Updating Value,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54543752/