nodejs调用Postgres使用in操作的方法

转载于:http://zhubangbang.com/the-implementation-method-of-nodejs-based-on-the-function-of-postgressql-package.html

在nodejs使用pgpg-pool,不能直接使用where来进行in操作;

比如在SQL的下面语句

select account from accounts where account in ('kangkang','tom')

转成where如下

pgclient.query(`select account from accounts where account in $1`, [['kangkang','tom']], (res) => {
    console.log("accounts", res)
});

但是这样写并不能正确的获取

可以使用ANY的写法

let names= ['kangkang', 'tom'];
let querySql = {
    text: "select account from accounts where account = ANY ($1)",
    values: [names]
};
 
pgclient.query(querySql, (res) => {
    console.log("accounts", res)
});
posted @ 2020-06-04 16:01  Mr_Kahn  阅读(377)  评论(0)    收藏  举报