nodejs基于postgresSql包的where in功能的实现方法

在nodejs使用的;

https://node-postgres.com/

不能直接使用where;

比如在SQL的下面语句

select account from accounts where account in 
('czr_3ijpzyoofukqrhjyqk5ma6c64m44fexc1idta7necszksbj6urfqq7yu3dsy','czr_1aptm6u579y1gx548hj7nmehtu3jmduxmp9pwm4abxyiartcd5kd4ofw1dyd')

转成where如下

let accAry = ['czr_3ijpzyoofukqrhjyqk5ma6c64m44fexc1idta7necszksbj6urfqq7yu3dsy', 'czr_1aptm6u579y1gx548hj7nmehtu3jmduxmp9pwm4abxyiartcd5kd4ofw1dyd'];
let upsertSql = {
    text: "select account from accounts where account in ($1)",
    values: [accAry]
};

pgclient.query(upsertSql, (res) => {
    console.log("accounts", res)
});

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

可以使用

let accAry = ['czr_3ijpzyoofukqrhjyqk5ma6c64m44fexc1idta7necszksbj6urfqq7yu3dsy', 'czr_1aptm6u579y1gx548hj7nmehtu3jmduxmp9pwm4abxyiartcd5kd4ofw1dyd'];
let upsertSql = {
    text: "select account from accounts where account = ANY ($1)",
    values: [accAry]
};

pgclient.query(upsertSql, (res) => {
    console.log("accounts", res)
});

写成ANY的语法即可

~~~~~

 

未经允许不得转载:朱邦邦的博客 » nodejs基于postgresSql包的where in功能的实现方法

赞 (0)

评论 0

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址