如何将 JSON 数组保存到 MySQL 数据库?

mysqlmysqli database

为此,您可以使用 MySQL 中的 JSON 数据类型。让我们首先创建一个 −

mysql> create table DemoTable1438
   -> (
   -> EmployeeDetails json
   -> );
Query OK, 0 rows affected (5.97 sec)

使用 insert 在表中插入一些记录 −

mysql> insert into DemoTable1438 values('[{"EmployeeId":"EMP-101","EmployeeName":"Chris"},{"EmployeeId":"EMP-102","EmployeeName":"David"},{"EmployeeId":"EMP-103","EmployeeName":"Sam"}]');
Query OK, 1 row affected (0.22 sec)

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

mysql> select * from DemoTable1438;

这将产生以下输出 −

+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| EmployeeDetails                                                                                                                                            |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+
| [{"EmployeeId": "EMP-101", "EmployeeName": "Chris"}, {"EmployeeId": "EMP-102", "EmployeeName": "David"}, {"EmployeeId": "EMP-103", "EmployeeName": "Sam"}] |
+------------------------------------------------------------------------------------------------------------------------------------------------------------+
1 row in set (0.00 sec)

相关文章