我有一张表(table1),列出了从最高年级到最低年级的学生名单。我想把它们分成3组。但是每组有多少学生?首先我计算我有多少学生,然后我在 table1 中查找列NumberStudent
等于学生总数的行(如果我找到的话)。我取 group1 的数量,这意味着 group1 中的学生人数。学生不应在其他组中重复。
表 2
包含根据他们的数量在每个组中有多少学生
-------------------------------------------
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
1 | 1 | 0 | 0 |
2 | 2 | 0 | 0 |
3 | 2 | 1 | 0 |
4 | 2 | 2 | 0 |
5 | 2 | 2 | 1 |
-----------------------------------------
- 对于 5 名学生,group1 = 2 名学生,group2 = 2 名学生和 group3 = 1 名学生
- 对于 3 名学生,group1 = 2 名学生,group2 = 1 名学生和 group3 = 0 名学生
表格1
+----+----------+------------+
| id | name | Marks |
+----+----------+------------+
| 1 | Bertrand | 17 |
| 2 | Charles | 10 |
| 3 | Alex | 12 |
| 4 | David | 11 |
| 5 | Eric | 20 |
| 6 | François | 20 |
| 7 | Gaston | 18 |
| 8 | Henri | 20 |
+----+----------+------------+
我想计算 Table1 中有多少学生
select count(Id) as Total from Table1
比如我有5个学生,我按照Table2的数量把他们分成3组。
表2
NumberStudent| group1 | group2 | group3 |
-----------------------------------------
5 | 2 | 2 | 1 |
我在 Group1 有 2 个学生,在 Group2 有 2 个学生,在 Group3 有 1 个学生
select name,Marks from Table1
where Marks >=10
order by Marks row //from table2 (how can i obtain the number row =2 as parameter )
Group2 有 2 个学生在 group1 中没有找到
select name,Marks from Table1
where Marks >=15 and id<> id // the student in groupe2 not mention in group1
order by Marks rowrow //from table1 (group2 for 5 student is 2 so row =2)
Group3 有 1 个学生,在 group1 中找不到
select name,Marks from Table1
where Marks >17 and id<> id // the student in groupe3 not mention in group1 and group2
order by Marks row row //from table1 (group3 for 5 student is 1 so row =1)
结果应该是
1 Henri 20 group1
2 Eric 20 group1
3 François 20 group2
4 Gaston 18 group2
5 Bertrand 17 group3