【问题标题】:Correct way of using node-postgres select with IN clause使用带有 IN 子句的 node-postgres 选择的正确方法
【发布时间】:2021-11-20 05:02:06
【问题描述】:

我正在尝试使用node-postgresin 子句查询中锻炼如何将逗号分隔的值字符串传递到 postgres 选择中,但不幸的是似乎无法让它工作并且可以'找不到任何例子。

我有以下代码:

function getUser() {
  let inList = "'qwerty', 'qaz'";

  const result = await pgPool.query("SELECT name FROM my_table WHERE info IN ($1)", [inList]);  
  if (result.rowCount == 1) return result.rows.map(row => row.name)
  return false
}

基本上希望最终结果查询处理:

SELECT name FROM my_table WHERE info IN ('qwerty', 'qaz')

不确定这是否真的是使用 pgPool 执行此操作的正确方法?

【问题讨论】:

    标签: node.js postgresql express node-postgres


    【解决方案1】:

    正确的方法在 Github 上的 FAQ --> https://github.com/brianc/node-postgres/wiki/FAQ#11-how-do-i-build-a-where-foo-in--query-to-find-rows-matching-an-array-of-values

    你应该可以这样做:

     pgPool.query("SELECT name FROM my_table WHERE info = ANY ($1)", [jsArray], ...);
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2021-08-26
      • 2014-11-16
      • 1970-01-01
      • 2019-11-11
      • 2013-07-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多