node.js - 复杂的过滤器 Sequelize js

标签 node.js sequelize.js

我有以下实体:

Product            [id, name]  
Properties         [id, name, product_id]  
Values             [id, name, property_id]  
Offer              [id, product_id]
OfferDetail        [offer_id, property_id, value_id]

我想在 sequelize 中执行以下过滤器:
product_id
[property_1, value_1], [property_2, value_2], ...

这将返回具有相应值的所有产品。

例子:
Products
[{1, Potato}, {2, Tomato}]

Properties
[{1, Color, 1}, {2, Size, 1}, {3, Size, 2}]

Values
[{1, White, 1}, {2, Red, 1}, {3, Small, 2}, {4, Big, 2}, {5, Medium, 2}, {6, Small, 3}, {7, Big, 3}]

Offer
[{1, 1}, {2, 1}, {3, 2}]   (I have 3 offers, 2 for potato and 1 for tomato)

OfferDetail
[{1, 1, 1}, {1, 2, 3}, {2, 1, 2}, {3, 3, 6}]  
meaning:
Potato: {Color White}, {Size Small}
Potato: {Color Red}
Tomato: {Size Small}

I want to be able to filter:
Potato Small
Potato (White or Red)

or a mix of them.

谢谢

最佳答案

对于这种类型的复杂查询,您可以尝试使用原始查询

select p.name, group_concat(distinct pr.name separator ',') as prNames, group_concat(distinct v.name separator ',') as prValues from OfferDetail od join Offer o on o.id=od.offer_id left join Properties pr on pr.id=od.property_id left join Values v on v.id=od.value_id left join Products p on p.id=o.product_id group by od.offer_id

你能检查一下这个查询吗?

关于node.js - 复杂的过滤器 Sequelize js,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40953180/

相关文章:

jquery - Angular 在 jQuery Ajax 加载页面中不起作用

javascript - Gulp - 从 src() 获取文件名

node.js - MongoDB和Nodejs插入自动递增的ID

node.js - 如何在nodeclipse中配置forever?

transactions - Node.js 7 如何将sequelize事务与async/await一起使用?

javascript - 在 Node.js 中使用 res.json 的 PhpStorm 未使用属性警告

node.js - findAll 没有根据 Includes Sequelize 获取对象结构

mysql - 我的 Firebase 云函数给出 SequelizeConnectionError : connect ETIMEDOUT error whenever I query my MySql DB

database - 无法连接到数据库 : SequelizeConnectionRefusedError: connect ECONNREFUSED

node.js - 如何在sequelize中正确使用子查询?