sequelize, 语句(where) in statement(where)
sequelize, Statement(where) in statement(where)
我正在尝试 2 个小时来解决一个小问题,但不是一个问题。
我正在使用生成的 yeoman Angular-fullstack App.
我想用sequelize写这段代码:
SELECT *
FROM demand
WHERE city_id NOT IN (
SELECT city_id
FROM demand
WHERE user_id=req.params.user_id)
我还有以下代码,但它不起作用。我只得到 []
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));
}
你有线索吗?
谢谢。
还没找到好的解决办法:终于写了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,我必须在我的页面上添加:
import db from '../../sqldb';
您的第一个查询存在问题,因为 sequelize returns promise 的 findall 方法。
在where条件下使用sequelize子查询的唯一方法是通过文字方法:sequelize.literal('your query');
查看此问题以获取更多信息https://github.com/sequelize/sequelize/issues/3961
我在我的项目中遇到了类似的问题。
我选择实现它的方式有点不同,原因有两个:
- 如果在某个时间点 Sequelize 决定实施子查询 - 语法就绪。
- 再次使用 Sequelize 保护 SQL 注入。
这是我的代码片段,希望对您有所帮助。
const tempSQL = sequelize.dialect.QueryGenerator.selectQuery('MyOtherTable',{
attributes: ['fkey'],
where: {
field1: 1,
field2: 2,
field3: 3
}})
.slice(0,-1); // to remove the ';' from the end of the SQL
MyTable.find( {
where: {
id: {
$notIn: sequelize.literal('(' + tempSQL + ')'),
}
}
} );
有些人可能会选择不使用 tempSQL 变量,而只是在 find 结构中构建 SQL(也许使用辅助方法?)
我还认为这可能是 sequelize 的子查询扩展的基础,因为它几乎使用相同的语法。
我正在尝试 2 个小时来解决一个小问题,但不是一个问题。 我正在使用生成的 yeoman Angular-fullstack App.
我想用sequelize写这段代码:
SELECT *
FROM demand
WHERE city_id NOT IN (
SELECT city_id
FROM demand
WHERE user_id=req.params.user_id)
我还有以下代码,但它不起作用。我只得到 []
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));
}
你有线索吗? 谢谢。
还没找到好的解决办法:终于写了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,我必须在我的页面上添加:
import db from '../../sqldb';
您的第一个查询存在问题,因为 sequelize returns promise 的 findall 方法。
在where条件下使用sequelize子查询的唯一方法是通过文字方法:sequelize.literal('your query');
查看此问题以获取更多信息https://github.com/sequelize/sequelize/issues/3961
我在我的项目中遇到了类似的问题。 我选择实现它的方式有点不同,原因有两个:
- 如果在某个时间点 Sequelize 决定实施子查询 - 语法就绪。
- 再次使用 Sequelize 保护 SQL 注入。
这是我的代码片段,希望对您有所帮助。
const tempSQL = sequelize.dialect.QueryGenerator.selectQuery('MyOtherTable',{
attributes: ['fkey'],
where: {
field1: 1,
field2: 2,
field3: 3
}})
.slice(0,-1); // to remove the ';' from the end of the SQL
MyTable.find( {
where: {
id: {
$notIn: sequelize.literal('(' + tempSQL + ')'),
}
}
} );
有些人可能会选择不使用 tempSQL 变量,而只是在 find 结构中构建 SQL(也许使用辅助方法?)
我还认为这可能是 sequelize 的子查询扩展的基础,因为它几乎使用相同的语法。