Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/joomla/2.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 如何一次获取数据_Java_Sql_Postgresql_Jdbc - Fatal编程技术网

Java 如何一次获取数据

Java 如何一次获取数据,java,sql,postgresql,jdbc,Java,Sql,Postgresql,Jdbc,我使用以下查询来获取使用jdbc的postgresql中一天的数据 int[] days={1,2,3,4,5,6,9,12,18,24} for(i=0;i<days.length;i++){ //connect to database String sql="select id,extract ('day' from time) as day,count(*) as count from public.data where time>='2016-10-'"+d

我使用以下查询来获取使用jdbc的postgresql中一天的数据

  int[] days={1,2,3,4,5,6,9,12,18,24}

for(i=0;i<days.length;i++){
  //connect to database
    String sql="select id,extract ('day' from time) as day,count(*) as count from public.data where time>='2016-10-'"+days[i]+'06:00:00' and  time<='2016-10-"+days[i]+" 09:00:00' group by id,day ";
  //execute sql and close connection
}
像这样,它将给予每一天。 但是这是每天都要做的,我正在将结果集作为csv来编写。取而代之的是,我可以得到整个结果集,其中包含6到9个周期内的每天计数,而不是执行许多sql语句吗?但是我希望所有的结果集都是这样的

id    day      count
___   ___      _____
 1     1         10
 2     1         12
 3     1         8
 1     2         10
 2     2         9
 3     2         18
 1     3         10
 2     3         12
 3     3         27

非常感谢您提供的任何帮助

您可以使用多功能一体使用过滤器,其中提取时间(day from time)在:天内,提取时间(hour from time)在6到8之间


:天用于hibernate查询中的参数映射,您可以使用sql的等效项。

首先您的查询有问题:

日期应介于两个
'
之间,而不是
'

您的查询应如下所示:

String sql="select id,extract ('day' from time) as day,count(*) as count 
            from public.data where time>='2016-10-"+days[i]+" 06:00:00' 
            and  time<='2016-10-"+days[i]+" 09:00:00' group by id,day ";
String sql=“选择id,从时间中提取('day'作为日期,计数(*)作为计数
来自public.data,其中时间>='2016-10-“+天[i]+“06:00:00”

时间您可以在一条语句中执行此操作,无需循环:

select id,
       extract(day from time) as day,
       count(*) as count 
from public.data 
where extract(day from time) = any (array[1,2,3,4,5,6,9,12,18,24] ) 
  and time::date between date '2016-10-01' and date '2016-10-31'
  and time::time between time '06:00:00' and time '09:00:00'
group by id,day 

我希望
time
不是该列的真实名称

您永远不必使用批处理。
String sql="select id,extract ('day' from time) as day,count(*) as count 
            from public.data where time>='2016-10-"+days[i]+" 06:00:00' 
            and  time<='2016-10-"+days[i]+" 09:00:00' group by id,day ";
select id,
       extract(day from time) as day,
       count(*) as count 
from public.data 
where extract(day from time) = any (array[1,2,3,4,5,6,9,12,18,24] ) 
  and time::date between date '2016-10-01' and date '2016-10-31'
  and time::time between time '06:00:00' and time '09:00:00'
group by id,day