pg-promise mulitple或在where子句中

我在pg-promise上遇到了一些麻烦。

我想在WHERE子句中指定一个未定义数目的参数。

所有参数将与OR连接。

 db.any( 'SELECT genre FROM genres'+ 'WHERE ( (genres.genre LIKE \'Animation\' OR genres.genre LIKE \'Action\') ') .then(function(data) { // success; }) .catch(function(error) { // error; }); }); 

我没有一个一个的指定参数,我有一个由用户传递的stream派的数组。

我想做这样的事情…

 var genres = ["Action", "Adventure", "Romantic"]; db.any( 'SELECT genre FROM genres'+ 'WHERE ( (genres.genre LIKE $1) '), [genres]) .then(function(data) { // success; }) .catch(function(error) { // error; }); }); 

我无法find一个方法来使其工作。

感谢您的帮助!

你的问题更多的是关于多个LIKE使用的PostgreSQL语法,而不是pg-promise 。 请参阅组合多个类似的查询 。

在你的情况最简单的是使用LIKE ANY(array[...])语法:

 const genres = ['Action', 'Adventure', 'Romantic']; db.any('SELECT genre FROM genres WHERE genre LIKE ANY($1)'), [genres]) .then(data => { // success, data = matching genres }) .catch(error => { // error }); }); 

这将格式化并执行:

 SELECT genre FROM genres WHERE genre LIKE ANY(array['Action', 'Adventure', 'Romantic'])