Can I relation multiple 'where' condition in pg-promise?

580 views Asked by At

I using pg-promise with express 4 on node 8.2.0. Inside that I want to concatenate where condition.

But, I couldn't find that way. Please help me.

I expect this.

let ids = [10, 20, 30]
db.query('SELECT * FROM table WHERE id = {something-way}', ids)

=> I expect to execute query like a SELECT * FROM table WHERE id = 10 & id = 20 & id = 30;

Question

Can I make query like it with pg-promise's methods? Or I must concatenate string?

Please lend me your wisdom.

1

There are 1 answers

0
vitaly-t On

It depends on the logic of your WHERE clause.

If the logic is to select all records where id is one of the values, then you can use :csv modifier supported by pg-promise, in combination with the WHERE IN clause:

const ids = [10, 20, 30];
db.query('SELECT * FROM table WHERE id IN ($1:csv)', [ids]);

And if it is something else, then again, it depends on the logic, which you would need to clarify in your question.