用JSON格式更新MySQL列?

要显示类似JSON格式的记录,请使用MySQL concat()。让我们首先创建一个表-

mysql> create table DemoTable1373
   -> (
   -> StudentId int NOT NULL AUTO_INCREMENT PRIMARY KEY,
   -> StudentDetails text
   -> );

使用insert命令在表中插入一些记录。在这里,我们没有插入任何内容-

mysql> insert into DemoTable1373 values();
mysql> insert into DemoTable1373 values();
mysql> insert into DemoTable1373 values();

使用select语句显示表中的所有记录-

mysql> select * from DemoTable1373;

这将产生以下输出-

+-----------+----------------+
| StudentId | StudentDetails |
+-----------+----------------+
|         1 | NULL           |
|         2 | NULL           |
|         3 | NULL           |
+-----------+----------------+
3 rows in set (0.00 sec)

以下是查询以JSON格式更新MySQL字段-

mysql> update DemoTable1373 set StudentDetails=concat("{" "StudentName:", " John ,"," StudentAge:", 21,","," StudentCountryName: "," US","} ");
Rows matched: 3  Changed: 3 Warnings: 0

让我们再次检查表记录-

mysql> select * from DemoTable1373;

这将产生以下输出-

+-----------+---------------------------------------------------------------+
| StudentId | StudentDetails                                                |
+-----------+---------------------------------------------------------------+
|         1 | {StudentName: John , StudentAge:21, StudentCountryName:  US}  |
|         2 | {StudentName: John , StudentAge:21, StudentCountryName:  US}  |
|         3 | {StudentName: John , StudentAge:21, StudentCountryName:  US}  |
+-----------+---------------------------------------------------------------+
3 rows in set (0.00 sec)