使用时间戳间隔的PHP Postgres查询

时间:2014-06-30 21:45:09

标签: php sql postgresql timestamp

我尝试使用2个时间戳运行查询。我不确定如何在" to"时间戳。我想补充一天,因此将包括03-31的23:59。如果有更好的方法来进行这种包容性搜索,请随时告诉我。

$from = "2014-01-01";
$to = "2014-03-31";

pg_prepare($db, 'my_query'
        , "select * from table where from >= $1 and to < $2 + interval '1' day");

$results = pg_execute($db,'my_query',array($from, $to));

2 个答案:

答案 0 :(得分:0)

pg_prepare($db, 'my_query', "
    select * 
    from table 
    where \"from\" >= $1 and to < $2::date + interval '1' day
");

由于from是保留字,因此必须加双引号。是的,您需要添加一天,以便包含03-31的整天。

答案 1 :(得分:0)

只需将integer添加到date(这不适用于timestamp):

pg_prepare($db, 'my_query', "
    select * 
    from table 
    where \"from\" >= $1 and \"to\" < $2::date + 1");

另外,@ Clodoaldo写的关于reserved wordsto的内容也被保留。
明智的将保留字用作标识符。

相关问题