tribe84 tribe84 - 3 months ago 86
MySQL Question

Sequelize: Concat fields in WHERE LIKE clause

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.

For instance, something like the following:

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

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);
});


Any ideas?

Answer

You'll need something like this

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

Note: untested

Original source

Comments