用whereIn缩短查询

时间:2018-03-20 10:28:54

标签: postgresql bookshelf.js

我有一个字符串和一个字符串数组来查询postgres数据库,如下所示

字符串:'70d8d32d-9ea0-4096-a40f-8df7ea5f1aeb' 和字符串数组 [ 'f2834712-f25c-41ee-80c1-f1e2b2ac1e07', '798670c5-606a-4a22-8961-2bad92ce9a25' ]

我必须通过单独的单个字符串循环数组和查询,并匹配数组中的每个字符串,这将产生这两个sql

首先查询:

select "id" 
from "TableA" 
where "card_id" = '70d8d32d-9ea0-4096-a40f-8df7ea5f1aeb' 
and "TableA"."location_id" = 'f2834712-f25c-41ee-80c1-f1e2b2ac1e07'

第二次查询:

select "id" 
from "TableA" 
where "card_id" = '70d8d32d-9ea0-4096-a40f-8df7ea5f1aeb' 
  and "TableA"."location_id" = '798670c5-606a-4a22-8961-2bad92ce9a25'

然后,这是我尝试使用bookshelf简化查询

TableA.query(qb => {
  qb.whereIn(
    `${TableA.prototype.tableName}.card_id`,
    '70d8d32d-9ea0-4096-a40f-8df7ea5f1aeb',
  );
  qb.whereIn(
    `${
      TableA.prototype.tableName
    }.location_id`,
    'f2834712-f25c-41ee-80c1-f1e2b2ac1e07', '798670c5-606a-4a22-8961-2bad92ce9a25',
  );
})
  .fetch({columns: ['id']})

但我不确定这是正确的做法

我可以使用whereIn在一个查询中简化它,因为我已经有了一个数组。

0 个答案:

没有答案
相关问题