javascript - Sequelize 复杂的连接查询以使用 findAll

标签 javascript mysql sequelize.js

所以我想为这 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    |       |

JctProductColors 表

| 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
   }
 });

我想做一个 findAll 来显示这个

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 - Sequelize 复杂的连接查询以使用 findAll,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36050524/

相关文章:

javascript - Distinct 和 Inner 与 sequelize 结合

sequelize.js - Sequelize CLI 找不到环境变量

Javascript - 如何访问按钮 onClick 中文本字段的值?

javascript - 如何知道浏览器选项卡是否已使用 Javascript 打开?

javascript - 在 javascript 项目中使用返回的 Neo4j 节点

php - 从 MySQL : TCPDF/FPDF or FDF? 创建 PDF 的最佳方法

mysql - 仅从第一个 order by 中获取一条记录,并从第二个 order by 中获取其余记录

javascript - 如何使用 javascript 检查密码中的特殊字符

mysql - 加入长文本字段

javascript - 使用 sequelize.js 给定一组 id 时的多次删除