tribe84 tribe84 - 1 year ago 602
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: [
where: {
is_active: 1
nest: false

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

Any ideas?

Answer Source

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

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download