以汇总方式连接不相关的表

vjrehmav  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(343)

我发现很难总结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_of_subject(where she/he scored lowest), 
col6 = lowest marks, 
col7 = faculty_Name(where she/he scored lowest)

注意-我只需要为给定的输出编写一个查询。
有四张table:
学生。
学生们正在学习这门学科。
教员。
标志。
您可以复制我的sql脚本中的代码来理解这些表。

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 ;

输出附加屏:

wvt8vs2t

wvt8vs2t1#

终于解决了这个问题!
汇总此表时需要做的基本调整是,子表必须与两列的组合联接,因为group by命令只反映汇总表中非汇总列的第一行值,因此无法同时反映max和min的值,我创建了子表,通过双列联接过滤行,最后将表联接到主student表。
加入的主桌是学生。
子表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新手来说,这可能是一个很好的练习。

相关问题