如何在mysql DB的sequelize中使用FIND_IN_SET

时间:2017-04-20 11:32:00

标签: mysql node.js sequelize.js

如何在续集ORM或sequelize中的任何等效物中使用FIND_IN_SET。

2 个答案:

答案 0 :(得分:0)

你会想要回到raw query。大多数ORM系统都不能很好地处理MySQL函数。

作为where查询的示例:

// This is *NOT* the best way to use FIND_IN_SET
sequelize.query("SELECT * FROM `users` WHERE FIND_IN_SET(username, 'foo,bar,baz') < 3", { type: sequelize.QueryTypes.SELECT})
  .then(users => {
      // only users with the name "foo" or "bar"
  })


// This is better, I guess.
sequelize.query("SELECT FIND_IN_SET(username, 'foo,bar,baz') as `idx`, id FROM `users`", { type: sequelize.QueryTypes.SELECT})
  .then(value => {
      console.log(value.id + ' has an idx of ' + value.idx);
  })

答案 1 :(得分:0)

FIND_IN_SET在连续行查询中的示例:

sequelize.query("select * from student where id in (10,2,12,9) ORDER BY FIND_IN_SET(id, '10,2,12,9')", { type: sequelize.QueryTypes.SELECT};

后续版本: 命令行:3.2.0 ORM:4.10.3