如何使用npm Sequqlize编写带左连接的嵌套where条件
我试图通过clubing实现左连接查询如何使用npm Sequqlize编写带左连接的嵌套where条件,npm,sequelize.js,Npm,Sequelize.js,我试图通过clubing实现左连接查询 select product.* from products left outer join cart as c on c.prodId = product.prodId left outer join order as order on c.cid = p.pid where product.secondaryId = 10 and c.cpid = 210; 我该如何编写,续写这方面的代码 编辑: select products.* from pr
select product.* from products
left outer join cart as c on c.prodId = product.prodId
left outer join order as order on c.cid = p.pid
where product.secondaryId = 10 and c.cpid = 210;
我该如何编写,续写这方面的代码
编辑:
select products.* from products
left outer join Cart as cart on cart.CartId = products.CartId
left outer join Orders as order on order.OrderId = cart.PartId
where cart.CartId = 1 and order.CustId = 12;
这里是实际的MYSQL查询,我要实现的是:
select products.* from products
left outer join Cart as cart on cart.CartId = products.CartId
left outer join Orders as order on order.OrderId = cart.PartId
where cart.CartId = 1 and order.CustId = 12;
续集结束时的关联
Cart.hasMany(models.Products, {as: 'Products', foreignKey: 'CartId'})
Cart.belongsTo(models.Orders, { as: 'Orders', foreignKey: 'PartId'})
Products.belongsTo(models.Cart, {onDelete: "CASCADE", foreignKey: { name: 'CartId', allowNull: false})
Products -> PrimaryKey: ProduceId
Cart -> PrimaryKey: CartId
Orders -> PrimaryKey: OrderId
首先,由于
c.cpid=210
的条件,c.prodId=product.prodId上的左侧外部联接购物车作为c将作为常规联接工作。将其删除或移动到子句上的,如下所示:
left outer join cart as c on c.prodId = product.prodId and c.cpid = 210
至于Sequelize查询,因为您没有显示您的模型定义和关联,所以我将尝试这样猜测和编写(这样您就可以了解整个想法):
如果您指出了模型定义和关联,那么我也可以更正我的答案(如果必要)。我只是在查询中为Order
添加了一个别名。根据您的模型定义和关联,这就是所需的全部内容