表结构:v_date为虚拟列CREATE TABLE `test` ( `json` TEXT NULL, `date` DATETIME NULL DEFAULT NULL, `v_date` DATE AS (json_extract(`json`,'$.date')) VIRTUAL)COMMENT='测试表\r\n'COLLATE='utf8mb4_general_ci'ENGINE=InnoDB;插入:INSERT INTO test (json) VALUES ('{"date":"2019-01-21 00:00:00"}');报错:ERROR 3156 (22018): Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column json_extract at row 1尝试了各种格式:INSERT INTO `test`.`test_virtual_column` (`json`) VALUES ('{"date":"20191221"}');/* SQL错误(3156):Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column json_extract at row 1 */INSERT INTO `test`.`test_virtual_column` (`json`) VALUES ('{"date":20191221}');/* SQL错误(3156):Invalid JSON value for CAST to DATE/TIME/DATETIME/TIMESTAMP from column
json_extract at row 1 */请问如何保存Date类型的数据到虚拟列中?
1 回答
慕雪6442864
TA贡献1812条经验 获得超5个赞
要先使用json_unquote()函数来去除JSON值的引号,建议新建虚拟列时都使用此函数。
CREATE TABLE `test_virtual_column` ( `json` JSON NULL DEFAULT NULL, `date` DATETIME NULL DEFAULT NULL, `v_date` DATETIME AS (json_unquote(json_extract(`json`,'$.date'))) VIRTUAL)
- 1 回答
- 0 关注
- 898 浏览
添加回答
举报
0/150
提交
取消