mysql - 如何在sequelize中使用where条件实现子选择
问题描述
我有这些表:
- 产品
- 商店
- 产品属性
有了这个结构
[
"products" :
{
"id": 1,
"orginalName": "146153-0100 ",
"title": null,
"stores": [
{
"id": 1,
"stock": 100,
"minOQ": 1,
"maxOQ": 0
},
{
"id": 2,
"stock": 100,
"minOQ": 1,
"maxOQ": 0,
}
],
"productproperties": [
{
"id": 1,
"productId": 1,
"propertyId": 8,
"propertyOptionId": 5
},
{
"id": 2,
"productId": 1,
"propertyId": 9,
"propertyOptionId": 11
},
{
"id": 3,
"productId": 1,
"propertyId": 10,
"propertyOptionId": 9
}
]
}
]
我想按选定的选项过滤我的产品,假设选定的选项是 11 和 9
如何在 Sequelize 5.6 中使用 findAll 、 where 和 ... 实现以下 sql 查询:
select * from products as p
inner join stores as sr on sr.productId = p.id
where (select count(*) from productProperties where propertyOptionId in (11,9) and productId = p.id) >= 2
解决方案
我发现在 sequelize 中使用查询生成器真的很令人困惑,所以如果你对原始 sql 很好,你可以按如下方式运行它们
如果 Student 是你的模型,那么 const students = Student.query('Select * from students');
推荐阅读
- extjs - 在 EXTJS 3.2.1 中按文本过滤网格
- ios - 从 Swift 4 的文件目录中删除用户选择的文件?
- java - 为什么 Comparator.comparingInt 在 Java 的 Arrays.sort() 中不起作用?
- php - 在 Laravel 应用程序中访问 CorePHP 应用程序会话时遇到的问题
- oracle - ORA-01422:和 ORA-06512:在接受 sql 配置文件时 - 优化顾问
- ios - 签署 IPA 文件并通过 Diawi 安装它
- sql - Sum(column_name) 和 sum(column_name) over (partition by id) 有什么区别
- reactjs - 在本机反应中使用 fcm 推送通知
- java - Hybris 更新问题 - azure.abstractbundler6307sn 表不存在
- javascript - css 焦点动画无法正常工作?