将复杂的联接查询转换为使用 findAll
Sequelize complex join query to use a findAll
所以我想为这 5 个表建立关系并执行 findAll 并从每个表中获取一些信息。对不起,表格显示丑陋
产品表
| Field | Type | Null | Key | Default | Extra |
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| typeId | int(11) unsigned | NO | MUL | NULL | |
| image | varchar(255) | YES | | NULL | |
| desc | text | YES | | NULL | |
| price | float | YES | | NULL | |
| stock | int(11) | YES | | NULL | |
类型表
| Field | Type | Null | Key | Default | Extra |
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
规格表
| Field | Type | Null | Key | Default | Extra |
| productId | int(11) unsigned | NO | PRI | NULL | |
| name | text | YES | | NULL | |
Jct产品颜色表
| Field | Type | Null | Key | Default | Extra |
| productId | int(11) unsigned | NO | PRI | NULL | |
| colorId | int(11) unsigned | NO | PRI | NULL | |
颜色表
| Field | Type | Null | Key | Default | Extra |
| id | int(11) unsigned | NO | PRI | NULL | auto_increment |
| name | varchar(255) | NO | | NULL | |
这就是我现在的关系
Product.belongsTo(Spec, {
"foreignKey": "id",
"through": {
model: "ProductSpec",
unique: false
},
"constraints": false
});
Spec.belongsTo(Product, {
"foreignKey": "productId",
"through": {
model: "ProductSpec",
unique: false
},
"constraints": false
});
Type.belongsToMany(Product, {
"constraints": false,
"foreignKey": "id",
"through": {
model: "ProductType",
unique: false
}
});
Product.belongsTo(Type, {
"constraints": false,
"foreignKey": "typeId",
"through": {
model: jctProductColor,
unique: false
}
});
Product.belongsToMany(Color, {
"constraints": false,
"foreignKey": "productId",
"through": {
model: jctProductColor,
unique: false
}
});
Color.belongsToMany(Product, {
"constraints": false,
"foreignKey": "colorId",
"through": {
model: jctProductColor,
unique: false
}
});
我想找一个全部显示这个
select types.name as Type, products.image, products.desc, products.price, products.stock, specs.name as Specs, colors.name as Color from products
join types
on types.id = products.typeId
join specs
on products.id = specs.productId
join jctproductcolors
on jctproductcolors.productId = products.id
join colors
on colors.id = jctproductcolors.colorid
where products.id = :id
我知道
你一年前问过这个问题,但我正在寻找同样的东西,发现了你未回答的问题。
你可以做这样的事情:
models.Product.findAll({
attributes: ['image', 'desc', 'price', 'stock'],
include: [{
model: models.Type,
attributes: [['name', 'Type']]
}, {
model: models.Specs,
attributes: [['name', 'Specs']]
}, {
model: models.JctProductColors,
include: [{
model: models.Color,
attributes: [['name', 'Color']]
}]
}
],
where: {
id: id
}
});
欲了解更多信息,请查看此处:
http://docs.sequelizejs.com/en/latest/docs/querying/
相关文章:
- 如何在JavaScript中将字符串转换为函数引用
- 如何在Javascript中将JSon对象转换为数组
- 使用JS将数组转换为json对象
- 如何使用json将对象列表从java转换为javascript
- 偶尔结结巴巴地说“;堆叠的”;translate()上的转换(v4.0.0-alpha40)
- 为什么在单独的函数中应用时转换会闪烁/断断续续(D3)
- 在Javascript中转换对象数组
- 将数字转换为一定数量的硬币
- 将纯文本URL转换为可单击链接
- 当图像转换为本地存储的DataURL时,EXIF被删除
- 如何使用js将SNAPSHOT内部版本号转换为3位数的整数
- 如何将TypeScript对象转换为普通对象
- 检测个位数整数时正在转换毫秒
- 从javascript到jquery的转换
- DOM事件通过JSON转换为java
- 将圆柱体转换为弯管
- 混合 ui-sref 和 $state.go 在 Angular ui-router 中进行状态转换
- JavaScript代码问题:我正在将对象转换为数组
- 如何将字母转换为二进制代码
- 将复杂的联接查询转换为使用 findAll