Asf*_*sdf 1 javascript sql node.js express
可以说我想用
WHERE ID=2134
但是,如果用户不提供该ID,则不应使用WHERE ID(因为它为null)
如何使用Sequelize处理此问题?
小智 11
Post.update({
updatedAt: null,
}, {
where: {
deletedAt: {
[Op.ne]: null
}
}
});
Run Code Online (Sandbox Code Playgroud)
小智 10
我读了标题,上面写着,如果不是空的。但是,我显示 IS NOT NULL 和 IS NULL
我附上一个例子。
const cars = await Car.findAll({
where: {
userId: {
[Op.in]: myUserIds, // array like userId IN [2, 3, 4]
},
action: 'start', // like: action = 'start'
sellDate: {
[Op.not]: null, // Like: sellDate IS NOT NULL
},
status: {
[Op.is]: null, // Like: status IS NULL
}
},
order: [
['id', 'DESC'] // Like: ORDER BY id DESC
],
limit: 5,
offset: 1
});
Run Code Online (Sandbox Code Playgroud)
有关详细信息,我附上了文件 node_modules/sequelize/types/lib/operators.d.ts
我希望能帮到你。
const cars = await Car.findAll({
where: {
userId: {
[Op.in]: myUserIds, // array like userId IN [2, 3, 4]
},
action: 'start', // like: action = 'start'
sellDate: {
[Op.not]: null, // Like: sellDate IS NOT NULL
},
status: {
[Op.is]: null, // Like: status IS NULL
}
},
order: [
['id', 'DESC'] // Like: ORDER BY id DESC
],
limit: 5,
offset: 1
});
Run Code Online (Sandbox Code Playgroud)
基本上你的查询应该有
WHERE fk_id=1234 OR fk_id is null
Run Code Online (Sandbox Code Playgroud)
所以对于sequilize它会
const Op = Sequelize.Op;
Model.findAll({
where: {
fk_id: {
[Op.or]: [1234, null]
}
}
});
Run Code Online (Sandbox Code Playgroud)
这是文档:http : //docs.sequelizejs.com/manual/tutorial/querying.html#operators
| 归档时间: |
|
| 查看次数: |
11891 次 |
| 最近记录: |