有没有一种方法可以查询一些您可能无法定义(不需要)的条件
const c = {
id?: number
type?: string
}
const sql = `SELECT * FROM smth WHERE id=$1 AND type=$2`
query(sql , [c.id, c.type])
答案 0 :(得分:2)
您可以使用
const sql = `SELECT * FROM smth WHERE ($1::int IS NULL OR id=$1) AND ($2::text IS NULL OR type=$2)`;
但是通常在这里,查询构建器库是合适的解决方案。