Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/postgresql/9.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在jdbc中为PostgreSQL设置“5天”(日期时间间隔)?_Java_Postgresql_Jdbc - Fatal编程技术网

Java 如何在jdbc中为PostgreSQL设置“5天”(日期时间间隔)?

Java 如何在jdbc中为PostgreSQL设置“5天”(日期时间间隔)?,java,postgresql,jdbc,Java,Postgresql,Jdbc,考虑下面的例子 select * from foo where time +'10 day' >current_timestamp 我想为Java参数化这个查询,但我不知道如何设置10天?!字符串不起作用您可以传递字符串参数并强制转换它,例如 select * from foo where time +'10 day'::interval >current_timestamp; select * from foo where (time + CAST(? AS interval)

考虑下面的例子

select * from foo where time +'10 day' >current_timestamp

我想为Java参数化这个查询,但我不知道如何设置10天?!字符串不起作用

您可以传递字符串参数并强制转换它,例如

select * from foo where time +'10 day'::interval >current_timestamp;
select * from foo where (time + CAST(? AS interval)) > current_timestamp
或者传递一个int参数乘以一个固定的间隔,如果您总是使用天而不是更复杂的间隔,这会更好。例如

select * from foo where (time + ? * INTERVAL '1' DAY) > current_timestamp

使用setInt参数。

请确保按如下方式编写查询:

SELECT * FROM foo WHERE "time" > current_timestamp - interval '1 day' * n
关键是将列与表达式进行比较,这样就可以使用索引

为当前时间戳传递一个容易计算的时间戳-间隔“10天”,或为n传递一个整数

我不会用时间作为列名,这是一个很好的例子


您是否尝试过从foo中选择*时间+10>当前时间戳?!?谢谢,亲爱的,很好的解决方案:想要在当前的时间戳中有参数吗?代替当前的时间戳。请帮助。。例如,选择*from foo WHERE time>TIMESTAMP-间隔“1天”;如何通过?在这种情况下,从JDBC@AshaKoshti:请以问题的形式提问-提供必要的上下文。