By using db<>fiddle, you agree to license everything you submit by Creative Commons CC0.
CREATE TABLE `categories_1` (
`Category1ID` int(11) NOT NULL AUTO_INCREMENT,
`Category1Name` varchar(45) DEFAULT NULL,
`Category1Name_FR` varchar(45) DEFAULT NULL,
`Category1Photo` varchar(45) DEFAULT NULL,
`Category1Order` int(3) DEFAULT NULL,
`Category1Status` int(1) DEFAULT 1,
PRIMARY KEY (`Category1ID`)
) ENGINE=InnoDB AUTO_INCREMENT=5 DEFAULT CHARSET=utf8 COMMENT='Top level category';
CREATE TABLE `categories_2` (
`Category2ID` int(11) NOT NULL AUTO_INCREMENT,
`Category2Name` varchar(45) DEFAULT NULL,
`Category2Name_FR` varchar(45) DEFAULT NULL,
`Category2Order` int(3) DEFAULT NULL,
`Category2Status` int(1) DEFAULT 1,
`Category1ID` int(11) DEFAULT NULL COMMENT 'To which parent level category it fits',
PRIMARY KEY (`Category2ID`)
) ENGINE=InnoDB AUTO_INCREMENT=15 DEFAULT CHARSET=utf8 COMMENT='Mid level category';
CREATE TABLE `categories_3` (
`Category3ID` int(11) NOT NULL AUTO_INCREMENT,
`Category3Name` varchar(45) DEFAULT NULL,
`Category3Name_FR` varchar(45) DEFAULT NULL,
`Category3Order` int(3) DEFAULT NULL,
`Category3Status` int(1) DEFAULT 1,
`Category2ID` int(11) DEFAULT NULL COMMENT 'To which parent level category it fits',
PRIMARY KEY (`Category3ID`)
) ENGINE=InnoDB AUTO_INCREMENT=20 DEFAULT CHARSET=utf8 COMMENT='Bottom level category';
INSERT INTO `categories_1` VALUES (1,'Meat','Meat-fr','meat.jpg',1,1),(2,'Fish & Sea Food',NULL,'fish.jpg',2,1),(3,'Fruit & Vegetables',NULL,'fruit-veg.jpg',3,1),(4,'Test L1',NULL,'categories-default.jpg',4,0);
INSERT INTO `categories_2` VALUES (1,'Beef','Feef-fr',1,1,1),(2,'Lamb',NULL,2,1,1),(3,'Pork',NULL,3,1,1),(4,'Veal',NULL,4,1,1),(5,'Poultry-Fowl',NULL,6,1,1),(6,'Sausages and Bacon',NULL,5,1,1),(7,'Salmon',NULL,8,1,2),(8,'Flat Fish',NULL,9,1,2),(9,'Common Fish',NULL,10,1,2),(10,'Squid family',NULL,11,1,2),(11,'Shellfish',NULL,12,1,2),(12,'Tuna',NULL,13,1,2),(13,'Other Fish',NULL,14,1,2),(14,'TEST L2',NULL,7,0,1);
INSERT INTO `categories_3` VALUES (1,'Specialist Beef','Specialist Beef-fr',1,1,1),(2,'Wagyu',NULL,2,1,1),(3,'Japanese Wagyu',NULL,3,1,1),(4,'Other Beef',NULL,4,1,1),(5,'All Lamb',NULL,6,1,2),(6,'All Pork',NULL,5,1,3),(7,'All Veal',NULL,7,1,4),(8,'All Poultry-Fowl',NULL,11,1,5),(9,'Pork Sausages',NULL,8,1,6),(10,'Other meats',NULL,9,1,6),(11,'Bacon',NULL,10,1,6),(12,'All Salmon',NULL,12,1,7),(13,'All Flat Fish',NULL,13,1,8),(14,'All Common Fish',NULL,14,1,9),(15,'All Squid family',NULL,15,1,10),(16,'All Shellfish',NULL,16,1,11),(17,'All Tuna',NULL,17,1,12),(18,'All Other Fish',NULL,18,1,13),(19,'TEST L3',NULL,999,0,14);
Records: 4 Duplicates: 0 Warnings: 0
Records: 14 Duplicates: 0 Warnings: 0
Records: 19 Duplicates: 0 Warnings: 0
SELECT * FROM categories_1;
SELECT * FROM categories_2;
SELECT * FROM categories_3;
Category1ID | Category1Name | Category1Name_FR | Category1Photo | Category1Order | Category1Status |
---|---|---|---|---|---|
1 | Meat | Meat-fr | meat.jpg | 1 | 1 |
2 | Fish & Sea Food | null | fish.jpg | 2 | 1 |
3 | Fruit & Vegetables | null | fruit-veg.jpg | 3 | 1 |
4 | Test L1 | null | categories-default.jpg | 4 | 0 |
Category2ID | Category2Name | Category2Name_FR | Category2Order | Category2Status | Category1ID |
---|---|---|---|---|---|
1 | Beef | Feef-fr | 1 | 1 | 1 |
2 | Lamb | null | 2 | 1 | 1 |
3 | Pork | null | 3 | 1 | 1 |
4 | Veal | null | 4 | 1 | 1 |
5 | Poultry-Fowl | null | 6 | 1 | 1 |
6 | Sausages and Bacon | null | 5 | 1 | 1 |
7 | Salmon | null | 8 | 1 | 2 |
8 | Flat Fish | null | 9 | 1 | 2 |
9 | Common Fish | null | 10 | 1 | 2 |
10 | Squid family | null | 11 | 1 | 2 |
11 | Shellfish | null | 12 | 1 | 2 |
12 | Tuna | null | 13 | 1 | 2 |
13 | Other Fish | null | 14 | 1 | 2 |
14 | TEST L2 | null | 7 | 0 | 1 |
Category3ID | Category3Name | Category3Name_FR | Category3Order | Category3Status | Category2ID |
---|---|---|---|---|---|
1 | Specialist Beef | Specialist Beef-fr | 1 | 1 | 1 |
2 | Wagyu | null | 2 | 1 | 1 |
3 | Japanese Wagyu | null | 3 | 1 | 1 |
4 | Other Beef | null | 4 | 1 | 1 |
5 | All Lamb | null | 6 | 1 | 2 |
6 | All Pork | null | 5 | 1 | 3 |
7 | All Veal | null | 7 | 1 | 4 |
8 | All Poultry-Fowl | null | 11 | 1 | 5 |
9 | Pork Sausages | null | 8 | 1 | 6 |
10 | Other meats | null | 9 | 1 | 6 |
11 | Bacon | null | 10 | 1 | 6 |
12 | All Salmon | null | 12 | 1 | 7 |
13 | All Flat Fish | null | 13 | 1 | 8 |
14 | All Common Fish | null | 14 | 1 | 9 |
15 | All Squid family | null | 15 | 1 | 10 |
16 | All Shellfish | null | 16 | 1 | 11 |
17 | All Tuna | null | 17 | 1 | 12 |
18 | All Other Fish | null | 18 | 1 | 13 |
19 | TEST L3 | null | 999 | 0 | 14 |
SELECT json_object( 'id', C1.Category1ID,
'text', C1.Category1Name,
'nodes', JSON_ARRAYAGG( JSON_OBJECT( 'id', C2.Category2ID,
'text', C2.Category2Name,
'nodes', C2.nodes,
'class', 'nav-level-2',
'href', 'admin-categories-2.php'
)
),
'class', 'nav-level-1',
'href', 'admin-categories-1.php'
) AS JSON
FROM categories_1 C1
LEFT JOIN ( SELECT C2.Category1ID,
C2.Category2ID,
C2.Category2Name,
JSON_ARRAYAGG( JSON_OBJECT( 'id', C3.Category3ID,
'text', C3.Category3Name,
'class', 'nav-level-3',
'href', 'admin-categories-3.php'
)
) as nodes
FROM categories_2 C2
LEFT JOIN categories_3 C3 ON C3.Category2ID = C2.Category2ID AND C3.Category3Status = 1
WHERE C2.Category2Status = 1
GROUP BY C2.Category2ID
-- ORDER BY C2.Category2Order, C3.Category3Order
) C2 ON C2.Category1ID = C1.Category1ID
WHERE C1.Category1Status = 1
GROUP BY C1.Category1ID
ORDER BY C1.Category1Order
;
JSON |
---|
{"id": 1, "text": "Meat", "nodes": "[{"id": 1, "text": "Beef", "nodes": "[\"{\\\"id\\\": 2, \\\"text\\\": \\\"Wagyu\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\",\"{\\\"id\\\": 3, \\\"text\\\": \\\"Japanese Wagyu\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\",\"{\\\"id\\\": 4, \\\"text\\\": \\\"Other Beef\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\",\"{\\\"id\\\": 1, \\\"text\\\": \\\"Specialist Beef\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 2, "text": "Lamb", "nodes": "[\"{\\\"id\\\": 5, \\\"text\\\": \\\"All Lamb\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 3, "text": "Pork", "nodes": "[\"{\\\"id\\\": 6, \\\"text\\\": \\\"All Pork\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 4, "text": "Veal", "nodes": "[\"{\\\"id\\\": 7, \\\"text\\\": \\\"All Veal\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 5, "text": "Poultry-Fowl", "nodes": "[\"{\\\"id\\\": 8, \\\"text\\\": \\\"All Poultry-Fowl\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 6, "text": "Sausages and Bacon", "nodes": "[\"{\\\"id\\\": 11, \\\"text\\\": \\\"Bacon\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\",\"{\\\"id\\\": 9, \\\"text\\\": \\\"Pork Sausages\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\",\"{\\\"id\\\": 10, \\\"text\\\": \\\"Other meats\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"}]", "class": "nav-level-1", "href": "admin-categories-1.php"} |
{"id": 2, "text": "Fish & Sea Food", "nodes": "[{"id": 7, "text": "Salmon", "nodes": "[\"{\\\"id\\\": 12, \\\"text\\\": \\\"All Salmon\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 8, "text": "Flat Fish", "nodes": "[\"{\\\"id\\\": 13, \\\"text\\\": \\\"All Flat Fish\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 9, "text": "Common Fish", "nodes": "[\"{\\\"id\\\": 14, \\\"text\\\": \\\"All Common Fish\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 10, "text": "Squid family", "nodes": "[\"{\\\"id\\\": 15, \\\"text\\\": \\\"All Squid family\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 11, "text": "Shellfish", "nodes": "[\"{\\\"id\\\": 16, \\\"text\\\": \\\"All Shellfish\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 12, "text": "Tuna", "nodes": "[\"{\\\"id\\\": 17, \\\"text\\\": \\\"All Tuna\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"},{"id": 13, "text": "Other Fish", "nodes": "[\"{\\\"id\\\": 18, \\\"text\\\": \\\"All Other Fish\\\", \\\"class\\\": \\\"nav-level-3\\\", \\\"href\\\": \\\"admin-categories-3.php\\\"}\"]", "class": "nav-level-2", "href": "admin-categories-2.php"}]", "class": "nav-level-1", "href": "admin-categories-1.php"} |
{"id": 3, "text": "Fruit & Vegetables", "nodes": "[{"id": null, "text": null, "nodes": null, "class": "nav-level-2", "href": "admin-categories-2.php"}]", "class": "nav-level-1", "href": "admin-categories-1.php"} |
-- subquery C2
SELECT C2.Category1ID,
C2.Category2ID,
C2.Category2Name,
JSON_ARRAYAGG( JSON_OBJECT( 'id', C3.Category3ID,
'text', C3.Category3Name,
'class', 'nav-level-3',
'href', 'admin-categories-3.php'
)
) as nodes
FROM categories_2 C2
LEFT JOIN categories_3 C3 ON C3.Category2ID = C2.Category2ID AND C3.Category3Status = 1
WHERE C2.Category2Status = 1
GROUP BY C2.Category2ID
Category1ID | Category2ID | Category2Name | nodes |
---|---|---|---|
1 | 1 | Beef | ["{"id": 2, "text": "Wagyu", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 3, "text": "Japanese Wagyu", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 4, "text": "Other Beef", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 1, "text": "Specialist Beef", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 2 | Lamb | ["{"id": 5, "text": "All Lamb", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 3 | Pork | ["{"id": 6, "text": "All Pork", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 4 | Veal | ["{"id": 7, "text": "All Veal", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 5 | Poultry-Fowl | ["{"id": 8, "text": "All Poultry-Fowl", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 6 | Sausages and Bacon | ["{"id": 9, "text": "Pork Sausages", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 10, "text": "Other meats", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 11, "text": "Bacon", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 7 | Salmon | ["{"id": 12, "text": "All Salmon", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 8 | Flat Fish | ["{"id": 13, "text": "All Flat Fish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 9 | Common Fish | ["{"id": 14, "text": "All Common Fish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 10 | Squid family | ["{"id": 15, "text": "All Squid family", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 11 | Shellfish | ["{"id": 16, "text": "All Shellfish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 12 | Tuna | ["{"id": 17, "text": "All Tuna", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 13 | Other Fish | ["{"id": 18, "text": "All Other Fish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
-- subquery C2
SELECT C2.Category1ID,
C2.Category2ID,
C2.Category2Name,
JSON_ARRAYAGG( JSON_OBJECT( 'id', C3.Category3ID,
'text', C3.Category3Name,
'class', 'nav-level-3',
'href', 'admin-categories-3.php'
)
) as nodes
FROM categories_2 C2
LEFT JOIN categories_3 C3 ON C3.Category2ID = C2.Category2ID AND C3.Category3Status = 1
WHERE C2.Category2Status = 1
GROUP BY C2.Category2ID
Category1ID | Category2ID | Category2Name | nodes |
---|---|---|---|
1 | 1 | Beef | ["{"id": 2, "text": "Wagyu", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 3, "text": "Japanese Wagyu", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 4, "text": "Other Beef", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 1, "text": "Specialist Beef", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 2 | Lamb | ["{"id": 5, "text": "All Lamb", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 3 | Pork | ["{"id": 6, "text": "All Pork", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 4 | Veal | ["{"id": 7, "text": "All Veal", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 5 | Poultry-Fowl | ["{"id": 8, "text": "All Poultry-Fowl", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
1 | 6 | Sausages and Bacon | ["{"id": 9, "text": "Pork Sausages", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 10, "text": "Other meats", "class": "nav-level-3", "href": "admin-categories-3.php"}","{"id": 11, "text": "Bacon", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 7 | Salmon | ["{"id": 12, "text": "All Salmon", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 8 | Flat Fish | ["{"id": 13, "text": "All Flat Fish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 9 | Common Fish | ["{"id": 14, "text": "All Common Fish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 10 | Squid family | ["{"id": 15, "text": "All Squid family", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 11 | Shellfish | ["{"id": 16, "text": "All Shellfish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 12 | Tuna | ["{"id": 17, "text": "All Tuna", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
2 | 13 | Other Fish | ["{"id": 18, "text": "All Other Fish", "class": "nav-level-3", "href": "admin-categories-3.php"}"] |
-- subquery C2 without GROUP BY and JSON_ARRAYAGG
SELECT C2.Category1ID,
C2.Category2ID,
C2.Category2Name,
JSON_OBJECT( 'id', C3.Category3ID,
'text', C3.Category3Name,
'class', 'nav-level-3',
'href', 'admin-categories-3.php'
)
as node
FROM categories_2 C2
LEFT JOIN categories_3 C3 ON C3.Category2ID = C2.Category2ID AND C3.Category3Status = 1
WHERE C2.Category2Status = 1
Category1ID | Category2ID | Category2Name | node |
---|---|---|---|
1 | 1 | Beef | {"id": 1, "text": "Specialist Beef", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 1 | Beef | {"id": 2, "text": "Wagyu", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 1 | Beef | {"id": 3, "text": "Japanese Wagyu", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 1 | Beef | {"id": 4, "text": "Other Beef", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 2 | Lamb | {"id": 5, "text": "All Lamb", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 3 | Pork | {"id": 6, "text": "All Pork", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 4 | Veal | {"id": 7, "text": "All Veal", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 5 | Poultry-Fowl | {"id": 8, "text": "All Poultry-Fowl", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 6 | Sausages and Bacon | {"id": 9, "text": "Pork Sausages", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 6 | Sausages and Bacon | {"id": 10, "text": "Other meats", "class": "nav-level-3", "href": "admin-categories-3.php"} |
1 | 6 | Sausages and Bacon | {"id": 11, "text": "Bacon", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 7 | Salmon | {"id": 12, "text": "All Salmon", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 8 | Flat Fish | {"id": 13, "text": "All Flat Fish", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 9 | Common Fish | {"id": 14, "text": "All Common Fish", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 10 | Squid family | {"id": 15, "text": "All Squid family", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 11 | Shellfish | {"id": 16, "text": "All Shellfish", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 12 | Tuna | {"id": 17, "text": "All Tuna", "class": "nav-level-3", "href": "admin-categories-3.php"} |
2 | 13 | Other Fish | {"id": 18, "text": "All Other Fish", "class": "nav-level-3", "href": "admin-categories-3.php"} |