我期待以下MySQL代码:
SET @json= '{"1":{"name":"Name","value":"James Bowery","id":1,"type":"name","first":"James","middle":"","last":"Bowery"},"2":{"name":"Birthdate","value":"06\/23\/2017","id":2,"type":"date-time","date":"06\/23\/2017","time":"","unix":1498176000},"3":{"name":"Gender","value":"Male","value_raw":"Male","id":3,"type":"radio"},"4":{"name":"Ethnicity","value":"European","value_raw":"European","id":4,"type":"radio"},"5":{"name":"Email","value":"jabowery@emailservice.com","id":5,"type":"text"}}';
SET @array = JSON_EXTRACT( @json, '$."1".first','$."1".last','$."5".value','$."2".value','$."3".value','$."4".value');
INSERT INTO user (firstname,lastname,birthdate,gender,ethnicity,email) VALUES (@array);
将行插入由JSON中提取的命名字段填充的user
表中。但是,INSERT
会产生语法错误。什么是正确的语法?
答案 0 :(得分:0)
也许试试这个。请注意,我已将日期格式更改为yyyy-MM-dd。您将需要以编程方式进行转换以适应MySQL。另外,当我运行它时,阵列在生日之前有电子邮件,所以我改变了插入序列。
SET @json= '{"1":{"name":"Name","value":"James Bowery","id":1,"type":"name","first":"James","middle":"","last":"Bowery"},"2":{"name":"Birthdate","value":"06\/23\/2017","id":2,"type":"date-time","date":"2017-06-23","time":"","unix":1498176000},"3":{"name":"Gender","value":"Male","value_raw":"Male","id":3,"type":"radio"},"4":{"name":"Ethnicity","value":"European","value_raw":"European","id":4,"type":"radio"},"5":{"name":"Email","value":"jabowery@emailservice.com","id":5,"type":"text"}}';
SET @array = JSON_EXTRACT( @json, '$."1".first','$."1".last','$."5".value','$."2".value','$."3".value','$."4".value');
SET @queryString= CONCAT( 'INSERT INTO user (firstname,lastname,email,birthdate,gender,ethnicity) VALUES (',REPLACE(REPLACE(@array, '[',''),']',''),')');
PREPARE stmt FROM @queryString;
EXECUTE stmt;
DEALLOCATE PREPARE stmt;