我有一个与PDO和Postgres有关的问题:
当我尝试通过float
字段使用integer
值进行搜索时出现错误:
$stmt = $con->prepare('SELECT t.* FROM my_table t WHERE t.integer_field > ?');
$stmt->execute([9.5]);
这也不起作用:
$stmt = $con->prepare('SELECT t.* FROM my_table t WHERE t.integer_field > ?');
$stmt->execute(['9.5']);
这也不起作用:
$stmt = $con->prepare('SELECT t.* FROM my_table t WHERE t.integer_field > ?');
$param = 9.5;
$stmt->bindParam(1, $param, \PDO::PARAM_INT);
$stmt->execute();
错误:Invalid text representation: 7 ERROR: invalid input syntax for integer: "9.5"
这是设计使然,还是postgres / pdo不兼容?
顺便说一句,当我内联不带引号的浮点数时,它会起作用:
$stmt = $con->prepare('SELECT t.* FROM my_table t WHERE t.integer_field > 9.5');
$stmt->execute();
postgres不会将类型转换为int,因为在运行这些查询时我看到了不同的记录:
$con->prepare('SELECT t.* FROM my_table t WHERE t.integer_field < 5.1');
$con->prepare('SELECT t.* FROM my_table t WHERE t.integer_field < 4.9');
#given that integer_field is 5
版本: PHP 7.2.19 PostgreSQL 10.4
答案 0 :(得分:0)
过一会儿,我发现了有效的查询:
$stmt = $con->prepare('SELECT t.* FROM my_table t WHERE CAST(t.integer_field AS FLOAT) > ?');
$stmt->execute([9.5]);
请,如果您有其他解决方案,请随时发布其他答案!