带有IN()参数的PostgreSQL PREPARE查询


10

我正在尝试从PHP准备查询,例如:

pg_prepare($con, "prep", "select * from test where tid in ($1)");

然后执行以下命令:

$strpar = "3,4,6,8,10";
pg_execute($con, "prep", array($strpars));

问题是我无法传递由于prepare需要固定数量的参数而构建的一系列值。有什么方法可以使参数动态化?

Answers:


15

使用数组表示一系列值:

pg_prepare($con, "prep", "select * from test where tid=ANY($1::int[])");

$strpar = "{3,4,6,8,10}";
pg_execute($con, "prep", array($strpars));

演员要int[]查询甚至可能是多余的,如果规划者能够通过自身来推断类型。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.