Sequelize 中的嵌套 SELECT 查询

标签 nested subquery sequelize.js

如何在 Sequelize 中使用受 SQL 注入(inject)保护的嵌套 SELECT 语句进行查询?

MySQL 条件查询的示例:

SELECT * FROM cities WHERE country_id IN (SELECT id FROM countries WHERE lang = 'French');

最佳答案

QueryGenerator.selectQuery()保存sql注入(inject):

const lang = 'French';
const subQuery = sequelize.dialect.QueryGenerator.selectQuery('countries',
    {
        attributes: ['id'],
        where: {
             lang: lang,
        }
    })
    .slice(0,-1); // to remove the ';' from the end of the SQL

CitiesModel.findAll( {
    where: {
        country_id: {
            [Op.in]: sequelize.literal('(' + subQuery + ')'),
        }
    } 
} );

关于Sequelize 中的嵌套 SELECT 查询,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65027205/

相关文章:

mysql - 复杂的mySQL 两表查询

ruby-on-rails - 如何将默认值合并到 Rails 5 中的嵌套参数中?

c# - aspose 嵌套表

mysql - 使用 JOIN 时如何优化 COUNT 个子查询

sql - 将 WHERE 语句添加到引用 ODBC 链接的子查询时访问查询失败

Node.js Sequelize 依赖问题

iphone - 嵌套 UITableView : a way to scroll both the outer vertical UITableView and a child horizontal UITableView at the same time?

listview - Flutter:当 GestureDetector 中有一个 ListView 时,它显然没有收到 Drag 事件

mysql - LEFT() 函数从其中的字符串中提取多个字符

sequelize.js - Sequelize ,使用 mysql 函数覆盖特定的列值