Sequelize:WHERE LIKE 子句中的 Concat 字段 [英] Sequelize: Concat fields in WHERE LIKE clause

查看:35
本文介绍了Sequelize:WHERE LIKE 子句中的 Concat 字段的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在为我正在处理的 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%'
    })
}

注意:未经测试

原始出处

这篇关于Sequelize:WHERE LIKE 子句中的 Concat 字段的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆