您好我知道我的实际问题看起来有点复杂,
所以首先我有 student
+------------+------- +---------+
| 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 |
+------------+--------+---------+
那么 class
+----------+--------------+
| id_class | limit_people |
+----------+--------------+
| 1 | 2 |
+----------+--------------+
| 2 | 2 |
+----------+--------------+
| 3 | 1 |
+----------+--------------+
最后一个让我们说 register . 在这个表上,学生可以选择2个 class ,但是当它按照2个分数( student.score_a
和 student.score_b
)DESC的总和按顺序排序时,它们只能在1个类(首先插入是优先级)(基于 class.limit_people
字段的限制)(这是均匀的)可能吗?),我知道这是否正确 . 我希望你能为此提供更好的领域/专栏
register
+-------------+-------------+----------+
| 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次,如果没有总结所有具有相同id_students的 total
,如何优先显示firt插入的id,如果有多个记录具有相同的 register.id_students
,如何使它只有1次?
这是我想要的表格
+-------------+-------------+-------+----------+
| id_register | id_students | total | id_class |
+-------------+-------------+-------+----------+
| 3 | 2 | 80 | 2 |
+-------------+-------------+-------+----------+
| 1 | 1 | 75 | 1 |
+-------------+-------------+-------+----------+
| 5 | 3 | 75 | 1 |
+-------------+-------------+-------+----------+
| 7 | 4 | 75 | 3 |
+-------------+-------------+-------+----------+
1 回答
使用
GROUP BY register.id_students
而不是GROUP by register.id_register
来获取id_students
字段的唯一值 . 所以查询将是kile以下: