我有这两张桌子:
categories
+----+----------------+
| ID | name |
+----+----------------+
| 1 | First category |
| 2 | Second category|
| 3 | Third category |
+----+----------------+
subcategories
+----+----------+----------------+
| ID | category | name |
+----+----------+----------------+
| 1 | 1 | 1.1 subcategory|
| 2 | 1 | 1.2 subcategory|
| 3 | 1 | 1.3 subcategory|
| 4 | 2 | 2.1 subcategory|
| 5 | 2 | 2.2 subcategory|
| 6 | 2 | 2.3 subcategory|
| 7 | 3 | 3.1 subcategory|
| 8 | 3 | 3.1 subcategory|
| 9 | 3 | 3.3 subcategory|
+----+----------+----------------+
我需要一个查询,它会得到如下结果:
Array( [0] => Array ( [category] = First category, [subcategory] => Array( [0] => "1.1 subcategory", [1] => "1.2 subcategory", [2] => "1.3 subcategory")), [1] => Array ( [category] = Second category, [subcategory] => Array( [0] => "2.1 subcategory", [1] => "2.2 subcategory", [2] => "2.3 subcategory")), [2] => Array ( [category] = Third category, [subcategory] => Array( [0] => "3.1 subcategory", [1] => "3.2 subcategory", [2] => "3.3 subcategory")))
我认为这与group by + join
有关我试过的查询
SELECT
categories.name,
subcategories.name
FROM categories
INNER JOIN
subcategories ON
categories.id = subcategories.category
GROUP BY categories.name
答案 0 :(得分:0)
我认为你应该使用2个SQL语句并重新构建数组。