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

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

我发现很难总结sql表。

  1. Objective: from the given tables I have to join and summarize the table.
  2. col1 = Name_of_student,
  3. col2 = Name_of_subject(where she/he scored highest),
  4. col3= highest_number,
  5. col4 = faculty_Name(where she/he scored highest),
  6. col5 = Name_of_subject(where she/he scored lowest),
  7. col6 = lowest marks,
  8. col7 = faculty_Name(where she/he scored lowest)

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

  1. create database university ;
  2. use university ;
  3. create table students (id int auto_increment primary key,
  4. student_name varchar(250) NOT NULL,
  5. dob DATE NOT NULL) ;
  6. create table faculty ( id int auto_increment primary key,
  7. faculty_name varchar(250) NOT NULL,
  8. date_of_update datetime default NOW()) ;
  9. create table Students_subject ( id int auto_increment primary key,
  10. subject_name varchar(250) default 'unknown' NOT NULL,
  11. subject_faculty int not null,
  12. foreign key(subject_faculty) references faculty(id));
  13. create table marks (id int auto_increment primary key,
  14. student_id int NOT NULL,
  15. subject_id int NOT NULL,
  16. marks int NOT NULL,
  17. date_of_update datetime default now() ON UPDATE NOW(),
  18. foreign key(student_id) references students(id),
  19. foreign key(subject_id) references students_subject(id));
  20. insert into students ( student_name, dob) values
  21. ('rob', '2001-03-06'),
  22. ('bbb', '2001-09-06'),
  23. ('rab', '1991-03-06'),
  24. ('root', '2001-03-16') ;
  25. insert into faculty(faculty_name) values
  26. ('kaka'),
  27. ('dope'),
  28. ('kallie'),
  29. ('kim');
  30. insert into students_subject (subject_name, subject_faculty) values
  31. ('maths', 2),
  32. ('physics', 3),
  33. ('english', 4),
  34. ('biology', 1),
  35. ('statistics', 2),
  36. ('french', 4),
  37. ('economics',3);
  38. insert into marks ( student_id, subject_id, marks) values
  39. (1,1,70),
  40. (1,2,60),
  41. (1,3,98),
  42. (1,4,75),
  43. (1,5,90),
  44. (1,6,30),
  45. (1,7,40),
  46. (2,1,70),
  47. (2,2,60),
  48. (2,3,70),
  49. (2,4,105),
  50. (2,5,95),
  51. (2,6,30),
  52. (2,7,10),
  53. (3,1,70),
  54. (3,2,60),
  55. (3,3,70),
  56. (3,4,75),
  57. (3,5,99),
  58. (3,6,30),
  59. (3,7,10),
  60. (4,1,70),
  61. (4,2,60),
  62. (4,3,70),
  63. (4,4,89),
  64. (4,5,99),
  65. (4,6,30),
  66. (4,7,19);

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

  1. select students.id, table_high.marks, table_high.faculty_name as high_faculty, table_high.subject_name as sub_high,
  2. student_low.marks , student_low.faculty_name as faculty_low, student_low.subject_name as sub_low from students
  3. inner join
  4. (select students.id, students.student_name ,marks.marks, subject_joined.faculty_name, students_subject.subject_name from marks
  5. inner join (select students_subject.id,students_subject.subject_name, faculty.faculty_name, students_subject.subject_faculty
  6. from students_subject left join faculty on students_subject.subject_faculty = faculty.id)
  7. as subject_joined on subject_joined.id = marks.subject_id
  8. inner join faculty on subject_joined.subject_faculty = faculty.id
  9. inner join students_subject on students_subject.id = marks.subject_id
  10. inner join students on students.id = marks.student_id
  11. order by 1, 3 desc) as table_high on table_high.id = students.id
  12. inner join
  13. (select students.id, students.student_name ,marks.marks, subject_joined.faculty_name, students_subject.subject_name from marks
  14. inner join (select students_subject.id,students_subject.subject_name, faculty.faculty_name, students_subject.subject_faculty
  15. from students_subject left join faculty on students_subject.subject_faculty = faculty.id)
  16. as subject_joined on subject_joined.id = marks.subject_id
  17. inner join faculty on subject_joined.subject_faculty = faculty.id
  18. inner join students_subject on students_subject.id = marks.subject_id
  19. inner join students on students.id = marks.student_id
  20. order by 1, 3 ) as student_low on student_low.id = students.id
  21. group by 1 ;

输出附加屏:

wvt8vs2t

wvt8vs2t1#

终于解决了这个问题!
汇总此表时需要做的基本调整是,子表必须与两列的组合联接,因为group by命令只反映汇总表中非汇总列的第一行值,因此无法同时反映max和min的值,我创建了子表,通过双列联接过滤行,最后将表联接到主student表。
加入的主桌是学生。
子表1-hw(总结了最高分的数据)子表1.2-最高分的标记。子表2-lw(汇总了最低值的表格)子表2.1-最低标记标记的低值。
查询>>

  1. select students.id, students.student_name, lw.min_marks, lw.lower_subject, lw.lower_faculty,
  2. hw.high_marks, hw.subject_name as high_subject, hw.faculty_name as higher_faculty
  3. from students inner join
  4. (select high.student_id, high.high_marks, high.subject_id, high.subject_name, high.faculty_name
  5. from
  6. (select marks.student_id, marks.marks as high_marks, sub_with_faculty.subject_id, sub_with_faculty.subject_name,
  7. sub_with_faculty.faculty_name from marks
  8. left join
  9. (select students_subject.id as subject_id, students_subject.subject_name, faculty.faculty_name
  10. from students_subject
  11. left join faculty on students_subject.subject_faculty = faculty.id) as sub_with_faculty
  12. on sub_with_faculty.subject_id = marks.subject_id) as high
  13. inner join (select marks.student_id, max(marks) as marks from marks group by 1) as maximum on
  14. maximum.student_id = high.student_id and maximum.marks = high.high_marks) as hw on
  15. hw.student_id = students.id
  16. inner join
  17. (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
  18. from
  19. (select marks.student_id, marks.marks as low_marks, sub_with_faculty.subject_id, sub_with_faculty.subject_name,
  20. sub_with_faculty.faculty_name from marks
  21. left join
  22. (select students_subject.id as subject_id, students_subject.subject_name, faculty.faculty_name
  23. from students_subject
  24. left join faculty on students_subject.subject_faculty = faculty.id) as sub_with_faculty
  25. on sub_with_faculty.subject_id = marks.subject_id) as low
  26. inner join (select marks.student_id, min(marks) as marks from marks group by 1) as minimum on
  27. minimum.student_id = low.student_id and minimum.marks = low.low_marks) as lw on
  28. lw.student_id = students.id;

对于像我这样的mysql新手来说,这可能是一个很好的练习。

展开查看全部

相关问题