使用多列制作续集WHERE IN

时间:2018-04-24 15:52:03

标签: mysql node.js sequelize.js

在MySQL(和其他dbs)中,you can do a where in query with multiple columns如下所示:

SELECT * FROM trains WHERE (name, location) IN (('train1', 'us'), ... ,('train2', 'us'));

我正在尝试用sequelize:

const selectTrains = [
    {name: 'train1', location: 'us'},
    {name: 'train1', location: 'eu'},
    {name: 'train2', location: 'us'},
];

let dbTrains = await Trains.findAll({ where: { [Op.in]: selectTrains} });

但是我收到以下错误:

  

UnhandledPromiseRejectionWarning:错误:值无效{name:'train1',location:'us'}

文档并未真正涵盖此用例。这可能与Op.in运算符有关吗?

1 个答案:

答案 0 :(得分:0)

您可以使用$or实现相同的目标:

const selectTrains = [
    {name: 'train1', location: 'us'},
    {name: 'train1', location: 'eu'},
    {name: 'train2', location: 'us'},
];

Trains.findAll({ where: { $or : selectTrains} });