My table structure in MySQL
CREATE TABLE `user_data` (
`id` int(11) NOT NULL,
`user` varchar(100) NOT NULL,
`data` longtext CHARACTER SET utf8mb4 COLLATE utf8mb4_bin NOT NULL CHECK (json_valid(`data`))
)
Table data
id | user | data |
---|---|---|
1 | 1 | {"box1": {"books": 12, "pen": 100},"box2": {"books": 13, "pen": 200},"box4": {"books": 17, "pen": 300},"box5": {"books": 16, "pen": 300}} |
2 | 5 | {"box1": {"books": 12, "pen": 100},"box2": {"books": 13, "pen": 200},"box3": {"books": 14, "pen": 300}} |
My expected results:
box | books | pen |
---|---|---|
box1 | 24 | 200 |
box2 | 26 | 400 |
box3 | 14 | 300 |
box4 | 17 | 300 |
box5 | 16 | 300 |
What is the MySQL select query to get this result?