这些是我的桌子:
Products
+-------------+---------+------+-----+
| Field | Type | Null | Key |
+-------------+---------+------+-----+
| id | int(11) | NO | PRI |
| retailer_id | int(11) | NO | MUL |
+-------------+---------+------+-----+
Clothings
+------------+-------------+------+-----+
| Field | Type | Null | Key |
+------------+-------------+------+-----+
| id | int(11) | NO | PRI |
| product_id | int(11) | NO | MUL |
| material | varchar(30) | YES | |
+------------+-------------+------+-----+
ClothingColors
+-------------+------------+------+-----+
| Field | Type | Null | Key |
+-------------+------------+------+-----+
| id | int(11) | NO | PRI |
| clothing_id | int(11) | NO | MUL |
| color | varchar(6) | NO | |
+-------------+------------+------+-----+
ClothingSizes
+------------------+-------------+------+-----+
| Field | Type | Null | Key |
+------------------+-------------+------+-----+
| id | int(11) | NO | PRI |
| clothingcolor_id | int(11) | NO | MUL |
| symbol | varchar(20) | NO | |
| availability | tinyint(1) | NO | |
| size_details | json | NO | |
+------------------+-------------+------+-----+
我需要一个查询结果:
[
{
"productId": 1,
"material": "explanation",
"variety": [
{
"colorId": 12,
"color": "ffffff",
"sizes": [
{ "sizeId": 33, "symbol": "L", "availability": true, "sizeDetails": {} },
{ "sizeId": 34, "symbol": "XL", "availability": true, "sizeDetails": {} }
],
}
]
},
{
"productId": 1,
"material": "explanation",
...
},
...
]
到目前为止,我可以提出以下查询:
SELECT Products.id AS productId,
GROUP_CONCAT(DISTINCT Clothings.material) AS material,
JSON_ARRAY(
GROUP_CONCAT(
JSON_OBJECT(
"colorId", ClothingColors.id,
"color", ClothingColors.color
"sizes", -- WHAT SHOULD BE HERE?
)
)
) AS variety
FROM Products
INNER JOIN Clothings ON Clothings.product_id = Products.id
INNER JOIN ClothingColors ON ClothingColors.clothing_id = Clothings.id
INNER JOIN ClothingSizes ON ClothingSizes.clothingcolor_id = ClothingColors.id
GROUP BY Products.id
问题:我该如何写一个查询,该查询会导致我的期望输出,并且由于我没有生产经验,您是否认为这将是一个缓慢的查询,而且最好在应用层?