我在mysql中有三个这样的表,
triz_sti
stu_id name
-----------------
1 x1
2 x2
triz_sub
sub_id sub_name
------------------
1 english
2 maths
3 science
triz
stu_id sub_id marks
-------------------------
1 1 23
1 2 56
1 3 83
2 1 78
2 2 23
2 3 50我想要的结果像显示所有科目中分数最高的科目和学生姓名,
max_marks sub_name student_name
--------------------------------------
78 english x2
56 maths x1
83 science x2所以请帮助这个输出,我想,我已经尝试,但我没有得到它想要的输出。
发布于 2011-08-26 12:56:37
像这样怎么样?
SELECT
t.stu_id, t.sub_id, t.marks
FROM
triz t
JOIN (SELECT sub_id, MAX(marks) max_mark FROM triz GROUP BY sub_id) a ON (a.sub_id = t.sub_id AND a.max_mark = t.marks)当然,您需要将其与名称的查找表连接起来。
不得不说,现在来的还早所以我可能漏掉了什么。
BR
发布于 2011-08-26 13:32:44
在这种情况下,通用的简化语法是
SELECT stuff FROM joined tables ORDER BY whatever最简单的是ORDER BY:您想要按标记降序排序,所以您使用ORDER BY marks DESC。
数据从何而来?来自triz,加入了其他人。所以
triz JOIN triz_sti USING (stu_id) JOIN triz_sub USING (sub_id)你想要显示这些标记。
所以你会得到
SELECT marks, sub_name, name AS student_name
FROM triz JOIN triz_sti USING (stu_id) JOIN triz_sub USING (sub_id)
ORDER BY marks DESC。
剩下的就交给你了。:-)
https://stackoverflow.com/questions/7200088
复制相似问题