我有以下表格:
Products:
+---------------------------+
|id|name |details |price|cn|
|__|_____|_________|_____|__|
| 1|pen |somethi |100 |10|
| 2|paper|something|30 |11|
+---------------------------+
Categories:
+----------------------------+
|id | name |parent_id |
|____________________________|
|1 | granny | 0 |
|2 | dad | 1 |
|3 | grandson | 2 |
|4 | grandson 2| 2 |
+----------------------------+
Products2categories:
+-------------------------------+
| id | product_id | category_id|
|_______________________________|
| 1 | 1 | 3 |
| 2 | 1 | 2 |
| 3 | 1 | 1 |
+-------------------------------+
我想做一个查询,该查询将返回与某些产品相关的所有类别。
例如:当我提供产品 ID 时,1
我想得到结果 grandson,dad,granny(与此产品相关的类别名称)
这是我的尝试:
SELECT `categories`.`name`
FROM `categories`
JOIN (
SELECT `products2categories`.`category_id`,`products2categories`.`product_id`
FROM `products2categories` a
JOIN `products`
ON `products`.`id` = `products2categories`.`product_id`
)
ON `categories`.`id` = `products2categories`.`category_id`
我有以下错误:
Every derived table must have its own alias
我想在这里得到一些帮助:)
提前致谢!