gpt4 book ai didi

java - 如何在java集合中做内循环?

转载 作者:行者123 更新时间:2023-11-30 11:32:02 27 4
gpt4 key购买 nike

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

最佳答案

这可以仅使用 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

关于java - 如何在java集合中做内循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16879734/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com