sequelize, Statement(where) in statement(where)

我尝试了 2 个小时来解决一个不是问题的小问题.我正在使用生成的 yeoman Angular-fullstack 应用程序.

I'm trying for 2 hours to resolve a little problem which is not one. I'm on an generated yeoman Angular-fullstack App.

我想用 sequelize 写这段代码:

I want to write this code with sequelize:

SELECT * 
FROM demand 
WHERE city_id NOT IN (
SELECT city_id
FROM demand
WHERE user_id=req.params.user_id)

我还有以下代码,但这不起作用.我只得到 []

I have yet the following code but that doesn't work. I get only []

export function getCity(req, res) {
  return Demand.findAll({
    where: {
      city_id:{ $notIn: Demand.findAll({
        attributes: ['city_id'],
        where: {
          user_id: req.params.user_id,
        }
      })}
    }
  })
  .then(handleEntityNotFound(res))
  .then(respondWithResult(res))
  .catch(handleError(res));
}

你有线索吗?谢谢.

推荐答案

我没有找到好的解决方案:我终于用查询写了.

I have not found a nice solution: I have finally wrote with query.

export function showDemandArtistNoUser(req, res) {
  return db.sequelize.query('SELECT * FROM demands WHERE city_id NOT IN (SELECT city_id FROM demands WHERE user_id='+req.params.user_id+')', { type: db.sequelize.QueryTypes.SELECT })
  .then(handleEntityNotFound(res))
  .then(respondWithResult(res))
  .catch(handleError(res));
}

而我的 angular-fullstack,我必须在我的页面上添加:

And with my angular-fullstack, i have to add on my page:

 import db from '../../sqldb';

相关文章