1

我在下面给出了 2 个表格

课程

classid int autoincrement primary key
classname varchar

学生

studentid int autoincrement primary key
studentname varchar
classid int foreign key references classes

我想在单个查询中交替地从选定班级的学生表中获取学生 ID。如果选择的classids是1,2,3,那么我想从classid 1然后2然后3再次从1开始然后2然后3再次从1开始等等。我如何编写查询以获得类似的结果

                studentid               classid
                  id1                      1
                  id2                      2
                  id3                      3
                  id4                      1
                  id5                      2
                  id6                      3
                  id7                      1      
4

2 回答 2

0

通过 Queryt 我不知道,但你可以这样做,

触发查询,您将交替获取所有 Id,例如 [5, 9, 10]。totalNumberOfIdsYouGot = 3(此处数组的总大小)

for(int i=0; i<numberOfTimesItShouldIterate; i++){
    for(int j=0; j<totalNumberOfIdsYouGot.length; j++){
        //Here fire single Query, change only ids each time as totalNumberOfIdsYouGot[j]           
    }
}
于 2013-07-25T05:42:11.250 回答
0

我不认为你可以在 Mysql 中做到这一点。Oracle 作为 connect by 语句,您可以通过它实现此目的。
我认为您将需要使用一个过程来使用此结果填充表格。然后选择
你可以使用两个游标并在里面实现这个Stored Procedure

于 2013-07-25T05:43:19.667 回答