如果行数达到指定字段的值,如何显示有限的记录

y4ekin9u  于 2021-06-21  发布在  Mysql
关注(0)|答案(1)|浏览(201)

正如一个有标题的人所说,这真的有可能吗?所以首先我有一个学生

+------------+------- +---------+
| id_student | score_a|score_b  |
+------------+--------+---------+
| 1          | 70     |80       |
+------------+--------+---------+
| 2          | 70     |90       |
+------------+--------+---------+
| 3          | 80     |70       |
+------------+--------+---------+
| 4          | 80     |70       |
+------------+--------+---------+
| 5          | 80     |90       |
+------------+--------+---------+
| 6          | 60     |70       |
+------------+--------+---------+
| 7          | 60     |80       |
+------------+--------+---------+
| 8          |40      |70       |
+------------+--------+---------+

然后上课

+----------+--------------+
| id_class | limit_people |
+----------+--------------+
| 1        | 1            |
+----------+--------------+
| 2        | 2            |
+----------+--------------+
| 3        | 2            |
+----------+--------------+

和寄存器表
登记

+-------------+-------------+----------+
| id_register | id_students | id_class |
+-------------+-------------+----------+
| 1           | 1           | 1        |
+-------------+-------------+----------+
| 2           | 1           | 2        |
+-------------+-------------+----------+
| 3           | 2           | 2        |
+-------------+-------------+----------+
| 4           | 2           | 3        |
+-------------+-------------+----------+
| 5           | 3           | 1        |
+-------------+-------------+----------+
| 6           | 3           | 3        |
+-------------+-------------+----------+
| 7           | 4           | 3        |
+-------------+-------------+----------+
| 8           | 4           | 2        |
+-------------+-------------+----------+

有没有一种方法,如果让我们说一个类达到最大限度的基础上 class.limit_people 字段,不能是包含这些id\u类的记录将不会再次显示,而是与其他id\u类一起显示,因为1个学生可以有2个id\u类?例如: class.limit_people = 1 为了 id_class = 1 ,在结果表中有2条记录,使用id\u class=1,不能只显示一次吗 total ?
当前查询:

SELECT 
  register.id_register, 
  register.id_students, 
  (student.score_a + student.score_b)*50/100 AS total,
  register.id_class 
FROM 
  `register` 
LEFT JOIN 
  `student` 
ON (register.id_students = student.id_student)
GROUP BY register.id_students 
ORDER BY total DESC

通过运行这些查询,结果是

+-------------+-------------+-------+----------+
| id_register | id_students | total | id_class |
+-------------+-------------+-------+----------+
| 3           | 2           | 80    | 2        |
+-------------+-------------+-------+----------+
| 1           | 1           | 75    | 1        |
+-------------+-------------+-------+----------+
| 5           | 3           | 75    | 1        |
+-------------+-------------+-------+----------+
| 7           | 4           | 75    | 3        |
+-------------+-------------+-------+----------+

如你所见 id_students = 3id_class=1 而是显示在表中(基于 class.limit_people ),我想要的结果就是 id_class=3 ```
+-------------+-------------+-------+----------+
| id_register | id_students | total | id_class |
+-------------+-------------+-------+----------+
| 3 | 2 | 80 | 2 |
+-------------+-------------+-------+----------+
| 1 | 1 | 75 | 1 |
+-------------+-------------+-------+----------+
| 6 | 3 | 75 | 3 | */ id_class = 3 instead of 1 since it's still not reach the max capacity
+-------------+-------------+-------+----------+
| 7 | 4 | 75 | 3 |
+-------------+-------------+-------+----------+

hgc7kmma

hgc7kmma1#

SELECT * FROM students WHERE score >=100 AND ROWNUM <= 3;

这只是为了回答一个问题谁在仅仅展示三排中得分最高。
0xB7BA52343F45927B9CEEAAB16228CA6EB9FE5A4型

相关问题