Is it possible to add a set of elements in one cell with MySQL?

MySQLMySQLi Database

To add a set of elements in a single cell, use the concept of JSON. Let us first create a table −

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

Insert some records in the table using insert command −

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)

Display all records from the table using select statement −

mysql> select * from DemoTable1828;

This will produce the following output −

+------------+----------------------------------------------------------------------------------------------+
| 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)
raja
Published on 24-Dec-2019 12:14:46
Advertisements