postgresql - 带有条件参数的 Node-postgres 准备好的语句
问题描述
有没有办法查询您有许多可能未定义的条件(不是必需的)
const c = {
id?: number
type?: string
}
const sql = `SELECT * FROM smth WHERE id=$1 AND type=$2`
query(sql , [c.id, c.type])
解决方案
你可以使用
const sql = `SELECT * FROM smth WHERE ($1::int IS NULL OR id=$1) AND ($2::text IS NULL OR type=$2)`;
但总的来说,这是查询构建器库是合适解决方案的地方。
推荐阅读
- javascript - 将字符串传递给事件侦听器函数
- amazon-web-services - Fargate 中的 StartTask V/S RunTask
- java - 为什么当 Comparator.compare 相等时我们需要返回 0
- node.js - 使用 DataDog 的舵图集成 DataDog GKE NESTJS
- c++ - 错误:运算符 [] 不匹配。在将 int 与列表中的 int 进行比较时
, - python - 要设置的 Python 排序列表
- bash - 从调用的 make 目标获取错误状态
- amazon-s3 - 如何使用用户上传的文件更新 .RDS 文件并将其存储在 Shiny(服务器)中以备下次使用?
- mongodb - Elasticsearch,MongoDb,Mongoosastic 查询对象数组
- oracle - 我无法在 oracle 11 g 中添加地理类型的列