你好,我知道我的问题,看起来有点复杂,
所以首先我有一个学生
+------------+------- +---------+
| 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 | 2 |
+----------+--------------+
| 2 | 2 |
+----------+--------------+
| 3 | 1 |
+----------+--------------+
最后让我们说注册。在这个表格中,学生可以选择2个班级,但他们只能选择1个班级(插入的第一个班级是优先顺序),当使用2个分数之和排序时( student.score_a
以及 student.score_b
)基于限制的描述 class.limit_people
field(这可能吗?),我会给你一张这张表,但我不知道这是否正确。我希望你能给出一个更好的字段/栏
登记
+-------------+-------------+----------+
| 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 |
+-------------+-------------+----------+
我使用的当前查询是
SELECT register.id_register, register.id_students, SUM((student.score_a*50/100)+ (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_register ORDER BY total DESC
这就是结果
+-------------+-------------+-------+----------+
| id_register | id_students | total | id_class |
+-------------+-------------+-------+----------+
| 3 | 2 | 80 | 2 |
+-------------+-------------+-------+----------+
| 4 | 2 | 80 | 3 |
+-------------+-------------+-------+----------+
| 2 | 1 | 75 | 2 |
+-------------+-------------+-------+----------+
| 5 | 3 | 75 | 1 |
+-------------+-------------+-------+----------+
| 6 | 3 | 75 | 3 |
+-------------+-------------+-------+----------+
| 7 | 4 | 75 | 3 |
+-------------+-------------+-------+----------+
| 8 | 4 | 75 | 2 |
+-------------+-------------+-------+----------+
| 1 | 1 | 75 | 1 |
+-------------+-------------+-------+----------+
正如你可以看到所有的学生都展示了2次,如何使它只有1而不求和所有 total
具有相同id的学生,如果有多个具有相同id的记录,如何优先显示第一个插入的id register.id_students
?
这是我想要的table
+-------------+-------------+-------+----------+
| id_register | id_students | total | id_class |
+-------------+-------------+-------+----------+
| 3 | 2 | 80 | 2 |
+-------------+-------------+-------+----------+
| 1 | 1 | 75 | 1 |
+-------------+-------------+-------+----------+
| 5 | 3 | 75 | 1 |
+-------------+-------------+-------+----------+
| 7 | 4 | 75 | 3 |
+-------------+-------------+-------+----------+
1条答案
按热度按时间xytpbqjk1#
使用
GROUP BY register.id_students
而不是GROUP by register.id_register
获取id_students
现场。因此,查询将如下所示: