可以使用 MySQL 向一个单元格中添加一组元素吗?


想在一个单元格中添加一组元素,可以使用 JSON。我们首先创建一个表格 -

mysql> create table DemoTable1828
     (
     EmployeeId int,
     EmployeeRecords JSON
     );
Query OK, 0 rows affected (0.00 sec)

使用 insert 指令在表中插入一些记录 -

mysql> insert into DemoTable1828 values(1,'[{"EmployeeName":"Chris","EmployeeAge":29},{"EmployeeName":"David","EmployeeAge":27}]');
Query OK, 1 row affected (0.00 sec)
mysql> insert into DemoTable1828 values(2,'[{"EmployeeName":"John","EmployeeAge":36},{"EmployeeName":"Mike","EmployeeAge":32}]');
Query OK, 1 row affected (0.00 sec)

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

mysql> select * from DemoTable1828;

这将生成以下输出 -

+------------+----------------------------------------------------------------------------------------------+
| EmployeeId | EmployeeRecords                                                                              |
+------------+----------------------------------------------------------------------------------------------+
|          1 | [{"EmployeeAge": 29, "EmployeeName": "Chris"}, {"EmployeeAge": 27, "EmployeeName": "David"}] |
|          2 | [{"EmployeeAge": 36, "EmployeeName": "John"}, {"EmployeeAge": 32, "EmployeeName": "Mike"}]   |
+------------+----------------------------------------------------------------------------------------------+
2 rows in set (0.00 sec)

更新于: 2019 年 12 月 24 日

49 次浏览

开启职业生涯

完成课程以获得认证

开始学习
广告