首页 > 解决方案 > 以汇总方式连接不相关的表

问题描述

我发现很难总结 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)

注意- 我只需要为给定的输出编写一个查询。

有四个表:

  1. 学生。
  2. 学生主题。
  3. 学院。
  4. 分数。

您可以复制我的 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); 

我自己编写了 Query 来解决这个问题,但不能打破它。

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 ;

附加输出屏幕:

截屏

标签: mysqlsqlmysql-workbench

解决方案


终于解决了这个问题!

汇总此表所需的基本调整是子表必须与两列的组合连接,因为 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 新手来说,这可能是一个很好的练习。


推荐阅读