javascript - SQL 分配无限参数
问题描述
通常我们会使用下面这样的 SQL 查询,我们将传递带有预定义数字$1的参数
queryRunner.query('SELECT * FROM sample_data WHERE code IN ($1)', ['1'])
但是我想在没有预先定义的情况下传递多个参数$1。有什么办法可以解决这个问题?
queryRunner.query('SELECT * FROM sample_data WHERE code IN ($$)', ['1','2','3'])
解决方案
One approach dynamically builds the IN
clause based on the expected number of parameters. Consider:
var params = ['1','2','3'];
var inClause = '?' + ', ?'.repeat(params.length - 1);
var sql = 'SELECT * FROM sample_data WHERE code IN (' + inClause + ')';
console.log(sql);
Once we have a statement with the right number of placeholders, we can simply bind the collection or array with no trouble.
推荐阅读
- postgresql - 无法使用应用程序堆栈构建器安装 pgagent
- html - 如何按类名解析内部 HTML,其中 html 分配给 Angular 7/Typescript 中的字符串变量
- angular - 折线图颜色全部变灰(ng2-charts)
- reactjs - 反应 JS + PDFKit
- javascript - 使用正则表达式将所有输入标签替换为其值
- python - 在 Python 中导航具有多个数组的 JSON
- highcharts - Highcharts 工具提示在使用 boost.js 时移动缓慢
- git - Github 操作,在分支上调度操作
- javascript - 点击事件打开一个带有特定项目的模式
- c - FatFS:f_findfirst 返回一个与找到的文件不对应的字符串