

新闻资讯
技术学院如何在 sequelize 中处理复杂的组合查询?
在 sequelize 中进行组合查询时,使用 where 子句并结合 op 操作符可以轻松实现。
为了生成复杂的查询条件,你可以按照以下步骤操作:
生成查询字符串:根据传入的参数( поле、值、操作符),使用 op 操作符生成 sql 查询字符串。例如:
if (_item[0] == 'and') {
if (_item[3] == 'equals') {
_where_arr[_item[1]] =
_item[2]
} else if (_item[3] == 'like') {
_where_arr[_item[1]] = {
[_this.sequelize_op.like]: '%' + _item[2] + '%'
}
}
}构建 where 对象:将生成的查询条件添加到 where 对象中。例如:
where = {'字段c' : "xxxx" , [op.and] : literal , '字段b':{ [op.like] : '%xxx%'} }使用 findone 或其他查询方法,where 对象作为参数传入:
let item = await Test.findOne(params);
提示: