如何在java集合中进行内部循环?

时间:2013-06-02 04:20:22

标签: java sql oracle jdbc

我的SQL返回如下所示的值。

enter code here

studentid   studentname   playid     gamename        grade        prizes   
---------  ------------   -----      ------------    ------      ---------   
121   bob                1          game1           A           1 and 2  
121   bob                2          game2           C           1 and 3  
121   bob                3          game3           B           4 and 2    
121   bob                4          game4           D           1 and 2  
131   jack               3          gam3            A           1    
131   jack               1          game1           A           2 and 3  

我正在获取结果并迭代要显示的值。但是在最后一列中需要将值显示为不同的格式。

Iterator<Search> iterator = products.iterator();   

while(iterator.hasNext())   
{   
Search req = (Search)iterator.next();   
req.getStudentid();   
req.getStudentname();   
req.getgameid();   
req.getgamename();   
req.getgrade();   
req.getprizes()          ;   
}  

显示格式为......

studentid  studentname    playid    gamename     grade        prizes   
---------- -----------    ------    --------    -------      ---------   
121           bob         1         game1        A           1 and 2 and 3 and 4  
121           bob         2         game2        C           1 and 2 and 3 and 4  
121           bob         3         game3        B           1 and 2 and 3 and 4    
121           bob         4         game4        D           1 and 2 and 3 and 4   
131           jack        3         gam3         A           1 and 2 and 3    
131           jack        1         game1        A           1 and 2 and 3 

如何在第一行追加奖品4行值?如何在这里循环? 请帮帮我。


编辑:我的SQL查询是:

SELECT stu.studentid, stu.studentname,g.playid,stu.gamename,g.grade,g.prizes
  FROM student stu , game g 
  WHERE stu.studentid = g.studentid AND stu.year = g.year

1 个答案:

答案 0 :(得分:1)

这可以仅使用SQL来完成,但我不确定它是否具有最佳性能。这可能应该在您的表示逻辑中处理。我还建议您考虑规范化您的奖品列。考虑存储这些1-n表(也许是GamePrizes)。

您正在尝试做一些事情。首先,您希望将所有奖品合并为一个值。您可以使用LISTAGG。但是,它不会包含不同的列表。因此,为了分解您的列表,您可以使用CONNECT BYREGEXP_SUBSTR将列表拆开 - 在这种情况下,我使用“和”作为分隔符。最后,再次使用LISTAGG将不同的奖品列表重新组合在一起,最后得到类似的结果:

select stu.studentid, stu.studentname, 
  g.playid, g.gamename,g.grade,
  listagg(allprizes, ' and ') within group (order by allprizes) allprizes
from student stu 
  join game g on stu.studentid = g.studentid and stu.year = g.year
  join (
    select distinct studentid, regexp_substr(allprizes,'[^ and ]+', 1, level) allprizes
    from 
    (
      select studentid, listagg(prizes, ' and ') within group (order by prizes) allprizes
      from game
      group by studentid
    ) 
    connect by regexp_substr(allprizes, '[^ and ]+', 1, level) is not null
  ) p on g.studentid=p.studentid
group by stu.studentid, stu.studentname, 
  g.playid, g.gamename,g.grade

导致:

STUDENTID   STUDENTNAME   PLAYID   GAMENAME   GRADE   ALLPRIZES
-------------------------------------------------------------------------
121         bob          1         game1      A       1 and 2 and 3 and 4
121         bob          2         game2      C       1 and 2 and 3 and 4
121         bob          3         game3      B       1 and 2 and 3 and 4
121         bob          4         game4      D       1 and 2 and 3 and 4
131         jack         1         game1      A       1 and 2 and 3
131         jack         3         game3      A       1 and 2 and 3