Sequelize:WHERE LIKE 子句中的 Concat 字段

2022-01-19 00:00:00 node.js mysql sequelize.js

我正在为我正在处理的 node.js 项目使用 sequelize ORM.我有一个查询,我需要对多列的连接结果执行类似的操作.

I am using the sequelize ORM for a node.js project I am working on. One query I have, I need to perform a like operation on the concatenated result of multiple columns.

例如,如下所示:

SELECT * FROM People WHERE (CONCAT(firstname, ' ', lastname)) LIKE '%John Do%'.

SELECT * FROM People WHERE (CONCAT(firstname, ' ', lastname)) LIKE '%John Do%'.

我正在使用以下语法,并且想知道这是否可行,而不必求助于使用 RAW 查询(这在我的解决方案中没有其他地方).

I am using the following syntax and would like to know if this is possible without having to resort to using RAW queries (which is nowhere else in my solution).

   var criteria = {
        include: [
            occupation
        ],
        where: {
            is_active: 1
        },
        nest: false
    };

    db.people.findAll(criteria, {}).then(function(people) {
        success(people);
    }).catch(function(err) {
        error(err);
    });

有什么想法吗?

推荐答案

你需要这样的东西

var criteria = {
    where: Sequelize.where(Sequelize.fn("concat", Sequelize.col("firstname"), Sequelize.col("lastname")), {
        like: '%John Do%'
    })
}

注意:未经测试

原始出处

相关文章