Mysql 以汇总方式连接不相关的表
我发现很难总结SQL表Mysql 以汇总方式连接不相关的表,mysql,sql,mysql-workbench,Mysql,Sql,Mysql Workbench,我发现很难总结SQL表 Objective: from the given tables I have to join and summarize the table. col1 = Name_of_student, col2 = Name_of_subject(where she/he scored highest), col3= highest_number, col4 = faculty_Name(where she/he scored highest), col5 = Name_o
Objective: from the given tables I have to join and summarize the table.
col1 = Name_of_student,
col2 = Name_of_subject(where she/he scored highest),
col3= highest_number,
col4 = faculty_Name(where she/he scored highest),
col5 = Name_of_subject(where she/he scored lowest),
col6 = lowest marks,
col7 = faculty_Name(where she/he scored lowest)
注意-我只需要为给定的输出编写一个查询
共有四个表格:
create database university ;
use university ;
create table students (id int auto_increment primary key,
student_name varchar(250) NOT NULL,
dob DATE NOT NULL) ;
create table faculty ( id int auto_increment primary key,
faculty_name varchar(250) NOT NULL,
date_of_update datetime default NOW()) ;
create table Students_subject ( id int auto_increment primary key,
subject_name varchar(250) default 'unknown' NOT NULL,
subject_faculty int not null,
foreign key(subject_faculty) references faculty(id));
create table marks (id int auto_increment primary key,
student_id int NOT NULL,
subject_id int NOT NULL,
marks int NOT NULL,
date_of_update datetime default now() ON UPDATE NOW(),
foreign key(student_id) references students(id),
foreign key(subject_id) references students_subject(id));
insert into students ( student_name, dob) values
('rob', '2001-03-06'),
('bbb', '2001-09-06'),
('rab', '1991-03-06'),
('root', '2001-03-16') ;
insert into faculty(faculty_name) values
('kaka'),
('dope'),
('kallie'),
('kim');
insert into students_subject (subject_name, subject_faculty) values
('maths', 2),
('physics', 3),
('english', 4),
('biology', 1),
('statistics', 2),
('french', 4),
('economics',3);
insert into marks ( student_id, subject_id, marks) values
(1,1,70),
(1,2,60),
(1,3,98),
(1,4,75),
(1,5,90),
(1,6,30),
(1,7,40),
(2,1,70),
(2,2,60),
(2,3,70),
(2,4,105),
(2,5,95),
(2,6,30),
(2,7,10),
(3,1,70),
(3,2,60),
(3,3,70),
(3,4,75),
(3,5,99),
(3,6,30),
(3,7,10),
(4,1,70),
(4,2,60),
(4,3,70),
(4,4,89),
(4,5,99),
(4,6,30),
(4,7,19);
我自己写了一封询问信来解决这个问题,但无法打破它
select students.id, table_high.marks, table_high.faculty_name as high_faculty, table_high.subject_name as sub_high,
student_low.marks , student_low.faculty_name as faculty_low, student_low.subject_name as sub_low from students
inner join
(select students.id, students.student_name ,marks.marks, subject_joined.faculty_name, students_subject.subject_name from marks
inner join (select students_subject.id,students_subject.subject_name, faculty.faculty_name, students_subject.subject_faculty
from students_subject left join faculty on students_subject.subject_faculty = faculty.id)
as subject_joined on subject_joined.id = marks.subject_id
inner join faculty on subject_joined.subject_faculty = faculty.id
inner join students_subject on students_subject.id = marks.subject_id
inner join students on students.id = marks.student_id
order by 1, 3 desc) as table_high on table_high.id = students.id
inner join
(select students.id, students.student_name ,marks.marks, subject_joined.faculty_name, students_subject.subject_name from marks
inner join (select students_subject.id,students_subject.subject_name, faculty.faculty_name, students_subject.subject_faculty
from students_subject left join faculty on students_subject.subject_faculty = faculty.id)
as subject_joined on subject_joined.id = marks.subject_id
inner join faculty on subject_joined.subject_faculty = faculty.id
inner join students_subject on students_subject.id = marks.subject_id
inner join students on students.id = marks.student_id
order by 1, 3 ) as student_low on student_low.id = students.id
group by 1 ;
附加输出屏幕:
终于解决了这个问题 汇总此表所需的基本调整是,子表必须与两列的组合联接,因为group by命令仅反映汇总表中非汇总列的第一行值,因此无法同时反映max和min的值,我创建了通过双列联接过滤行的子表,并最终将该表联接到主学生表 加入的主表是学生 子表1-hw(其中总结了最高等级的数据) 子表1.2-最高标记的高标记。 子表2-lw(汇总了最低值的表) 子表2.1-最低标记标记低 查询>>
select students.id, students.student_name, lw.min_marks, lw.lower_subject, lw.lower_faculty,
hw.high_marks, hw.subject_name as high_subject, hw.faculty_name as higher_faculty
from students inner join
(select high.student_id, high.high_marks, high.subject_id, high.subject_name, high.faculty_name
from
(select marks.student_id, marks.marks as high_marks, sub_with_faculty.subject_id, sub_with_faculty.subject_name,
sub_with_faculty.faculty_name from marks
left join
(select students_subject.id as subject_id, students_subject.subject_name, faculty.faculty_name
from students_subject
left join faculty on students_subject.subject_faculty = faculty.id) as sub_with_faculty
on sub_with_faculty.subject_id = marks.subject_id) as high
inner join (select marks.student_id, max(marks) as marks from marks group by 1) as maximum on
maximum.student_id = high.student_id and maximum.marks = high.high_marks) as hw on
hw.student_id = students.id
inner join
(select low.student_id, low.low_marks as min_marks, low.subject_id as lower_subjectID, low.subject_name as lower_subject, low.faculty_name as lower_faculty
from
(select marks.student_id, marks.marks as low_marks, sub_with_faculty.subject_id, sub_with_faculty.subject_name,
sub_with_faculty.faculty_name from marks
left join
(select students_subject.id as subject_id, students_subject.subject_name, faculty.faculty_name
from students_subject
left join faculty on students_subject.subject_faculty = faculty.id) as sub_with_faculty
on sub_with_faculty.subject_id = marks.subject_id) as low
inner join (select marks.student_id, min(marks) as marks from marks group by 1) as minimum on
minimum.student_id = low.student_id and minimum.marks = low.low_marks) as lw on
lw.student_id = students.id;
对于像我这样的MySQL新手来说,这可能是一个很好的练习。请使用google
Group By
和聚合函数。尝试一些查询,并编辑问题以添加您的最新尝试。Stackoverflow上的Welkom,Stackoverflow不是一个免费的编码服务或家庭作业、求职面试帮助网站。。我们愿意帮助您,如果您自己至少显示一个尝试查询。。。这是如何在Stackoverflow指南中提出的问题,该指南是从该链接获得的,“解释你如何遇到你试图解决的问题,以及任何阻碍你自己解决问题的困难。”亲爱的雷蒙德,以下问题是我无法解决的自我思考问题。这既不是基于家庭作业的问题,也不是为了解决而在网站上发布的问题。我已经尝试了很多次,但不能用一个准确的问题来解决问题。我也搜索了很多解决方案,但找不到一个适合我的问题,尽管我没有发布我的意见,我认为这听起来可能很可笑。您现在可以看一看了。@MohammedTaherPansari您有权访问MySQL服务器版本>=8.0.2吗?这是一个窗口函数问题。还可以编辑问题并添加预期的输出值(根据您的样本数据)。@MadhurBhaiya,我已经编辑了问题并添加了样本输出截图以供参考。谢谢