[英]Query and table - MAX and Join
Still very new to all of this so bear with me. 所有这些仍然很新,所以请耐心等待。
Have 3 tables 有3张桌子
table 1: member
Mem_index, Mem_name
1 joe
2 Mark
Table 2: Course
Course_index, Course_Name
1 Math
2 Reading
Table 3 : data
Data index,Member,Course,Score
1 1 1 85
2 1 2 75
3 2 1 95
4 1 2 65
SO what I would like to do is create a table: Do a query and gather all of the courses, find the max score for each course and attribute the member name to it. 所以我想要做的是创建一个表:执行查询并收集所有课程,找到每个课程的最高分数并将成员名称归属于它。
Table result should look like: 表结果应如下所示:
Course, Max score,name
Math 95 Mark
Reading 75 Mark
I can do the query individually but unsure of how to loop it and then propogate the data into the table. 我可以单独进行查询,但不确定如何循环查询,然后将数据传播到表中。
How about this query for SQL? 这个SQL查询怎么样?
SELECT c.course_name, MAX( d.score ), m.mem_name
FROM members m
JOIN data d on m.mem_id = d.member
JOIN course c on c.course_id = d.course
GROUP BY d.course
ORDER BY d.score, m.mem_name, c.course_name
Not sure if the field names match up but you get the idea - tested this in sql with some dummy data. 不确定字段名称是否匹配,但你得到了想法 - 在sql中使用一些虚拟数据进行测试。
Data
Index Member Course Score
1 1 1 60
1 1 1 85
Course
course_id course_name
1 Math
2 English
3 Science
Members
mem_id mem_name
1 Mark
2 James
You will get the following 您将获得以下内容
Course Name Score Member
Math 85 Mark
Try this query : 试试这个查询:
SELECT c.course_Name , MAX(d.score),m.mem_name
FROM data d
JOIN course c ON d.course=c.course_index
JOIN members m ON m.mem_index = d.member
GROUP BY d.course
ORDER by MAX(d.score) DESC
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.