2

我的 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
4

1 回答 1

1

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

您正在尝试做一些事情。首先,您想将所有奖品组合成一个值。你可以使用LISTAGG它。但是,它不会包含不同的列表。因此,要拆分您的列表,您可以使用CONNECT BYandREGEXP_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
于 2013-06-02T05:06:25.427 回答