我有类似的表格:
汽车:
+-------+-------------+
| carId | title |
+-------+-------------+
| car1 | car_1_title |
+-------+-------------+
类别:
+------------+------------------+
| categoryId | title |
+------------+------------------+
| category1 | category_1_title |
+------------+------------------+
子类别:
+---------------+------------+---------------------+
| subcategoryId | categoryId | title |
+---------------+------------+---------------------+
| subcategory1 | category1 | subcategory_1_title |
+---------------+------------+---------------------+
| subcategory2 | category1 | subcategory_2_title |
+---------------+------------+---------------------+
汽车子类别:
+---------------+------------+-------+
| subcategoryId | categoryId | carID |
+---------------+------------+-------+
| subcategory1 | category1 | car1 |
+---------------+------------+-------+
| subcategory2 | category1 | car1 |
+---------------+------------+-------+
我的目标是获取像这样的对象
[{
carId: 'car1',
title: 'car_1_title',
categories: [{
categoryId: 'category1',
title: 'category_1_title'
subcategories: [
{
subcategoryId: 'subcategory1',
title: 'subcategory_1_title'
},
{
subcategoryId: 'subcategory2',
title: 'subcategory_2_title'
}
]
}]
}]
如果我有sequalize
,用carId
来获得它的最好方法是什么?