question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

How could I use the sequelize.fn with the MySQL function `FIND_IN_SET`

See original GitHub issue

I have a query and an update query using the MySQL Function FIND_IN_SET, but the manual does not file a usage aboud this. The query like:

sequelize.query('SELECT * FROM house WHERE FIND_IN_SET(:uid, users)',
    {replacements: {uid: uid},
    type: sequelize.QueryTypes.SELECT}).then(function (houses) {
})

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:10 (4 by maintainers)

github_iconTop GitHub Comments

10reactions
liloewcommented, May 2, 2017

@sushantdhiman Thx for your reply, the below is I’m using in my project:

model.findAndCountAll({
    where: sequelize.where(sequelize.fn('FIND_IN_SET', uid, sequelize.col('users')), '>', 0)
})
8reactions
sushantdhimancommented, Nov 22, 2018
const Sequelize = require('./index');
const sequelize = require('./test/support').createSequelizeInstance();

const User = sequelize.define('user', {
  name: Sequelize.STRING
});

sequelize.sync({}).then(() => {
  return User.build({ id: 1, name: 'abc' }).save().then(() => {
    return User.findAll({
      where: {
        name: { [Sequelize.Op.ne]: 'a' },
        [Sequelize.Op.and]: [
          sequelize.where(sequelize.fn('random'), Sequelize.Op.ne, 0)
        ]
      },
      logging: true
    });
  });
}).then(r => {
  console.log(JSON.stringify(r, null, 2));
});
Executing (default): SELECT `id`, `name`, `createdAt`, `updatedAt` FROM `users` AS `user` WHERE (random() != 0) AND `user`.`name` != 'a';

[
  {
    "id": 1,
    "name": "abc",
    "createdAt": "2018-11-22T12:50:59.140Z",
    "updatedAt": "2018-11-22T12:50:59.140Z"
  }
]
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to use FIND_IN_SET in sequelize with mysql DB
An example for the FIND_IN_SET in sequelize row query: sequelize.query("select ...
Read more >
Node.js MySQL FIND_IN_SET() Function - GeeksforGeeks
FIND_IN_SET () function is a built-in function in MySQL that is used to get the position of the first occurrence of value string...
Read more >
Model Querying - Basics - Sequelize
You can use sequelize.fn to do aggregations: Model.findAll({ attributes: [ 'foo', [sequelize.fn('COUNT', sequelize.col('hats')), 'n_hats'],
Read more >
MySQL FIND_IN_SET Function with Practical Examples
In this tutorial, you will learn how to use the MySQL FIND_IN_SET function to return the position of a string in a comma-separated...
Read more >
Querying - Manual | Sequelize
findAll({ attributes: [[sequelize.fn('COUNT', sequelize.col('hats')), 'no_hats']] }); SELECT COUNT(hats) AS no_hats ... When using aggregation function, ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found