table product.combinations
Column 1: product.id
Column 2: attribute.id
Product.id是独一无二的 每个产品都可以有1个或多个属性。 没有属性的产品不在表格中
例如
table product.combinations
1 | 1 |
1 | 2 |
2 | 1 |
2 | 2 |
5 | 1 |
5 | 3 |
9 | 2 |
9 | 3 |
9 | 5 |
现在我想用以下结果进行选择
1 | 1 | 2 | | |
2 | 1 | 2 | | |
5 | 1 | 3 | | |
9 | 2 | 3 | 5 | |
我已经尝试过一个支点,但我无法获得好结果。 谁能给我一个帮助?
答案 0 :(得分:1)
首先:不要在表/列名称中使用点/句点,这不起作用,而是使用下划线。
其次,如果可以在逗号分隔列表中包含所有属性ID,则可以执行以下操作(使用MySQL的GROUP_CONCAT
函数):
mysql> SELECT
-> product_id, GROUP_CONCAT(attribute_id ORDER BY attribute_id) AS attributes
-> FROM product_combinations
-> GROUP BY product_id
-> ORDER BY product_id;
+------------+---------------+
| product_id | attributes |
+------------+---------------+
| 1 | 1,2 |
| 2 | 1,2 |
| 5 | 1,3 |
| 9 | 2,3,5 |
+------------+---------------+
4 rows in set (0.00 sec)
答案 1 :(得分:-1)
SELECT ATTRIBUTE 从组合 UNION ALL 选择属性 来自组合