Mysql joint table query (student table, teacher table, score table, curriculum table)

1 Library statement

create database if not exists studentinfo character set utf8mb4;

2 Table building statement

Insert picture description here

A student can choose multiple courses, and a course corresponds to multiple students, so a relationship table (student_course) is needed. For convenience, in addition to putting student id and course id into student_course, also put the grades into the relation table together. The other course corresponds to a teacher.

(1) Student table (student)

create table student(
 s_id int,
 sname varchar(20),
 sage int,
 sgender varchar(8)
)

(2) Course schedule

create table course(
  c_id int,
  cname varchar(20),
  t_id int
)

(3) Relationship table (student_course)-relationship table between students and courses

create table student_course(
 s_id int, # 学生id
 c_id int, # 课程id
 score int # 成绩
)

(4) Teacher table (teacher)

create table teacher(
	t_id int,  #老师id
	tname varchar(20)  # 老师姓名
)

3 Insert test data

(1) Insert student data

insert into student select 1,N'刘一',18,N'男' union all
 select 2,N'钱二',19,N'女' union all
 select 3,N'张三',17,N'男' union all
 select 4,N'李四',18,N'女' union all
 select 5,N'王五',17,N'男' union all
 select 6,N'赵六',19,N'女' ;

note:

  • N indicates that the subsequent string is a Unicode character.
  • Union: Union operation of two result sets, excluding duplicate rows, and sorting by default rules at the same time;
  • Union All: Perform union operation on two result sets, including duplicate rows, without sorting;
  • Note that you can use select when using insert (you need to ensure that the query field is consistent with the inserted field)
insert into student values(1,’张三’,1203);
insert into test_user(name, email) select name, qq_mail from student;

(2) Insert teacher data

 insert into teacher select 1,N'叶平' union all
 select 2,N'贺高' union all
 select 3,N'杨艳' union all
 select 4,N'周磊';

(3) Insert course data

 insert into course select 1,N'语文',1 union all
 select 2,N'数学',2 union all
 select 3,N'英语',3 union all
 select 4,N'物理',4;

(4) Insert relational table data

insert into student_course 
 select 1,1,56 union all 
 select 1,2,78 union all 
 select 1,3,67 union all 
 select 1,4,58 union all 
 select 2,1,79 union all 
 select 2,2,81 union all 
 select 2,3,92 union all 
 select 2,4,68 union all 
 select 3,1,91 union all 
 select 3,2,47 union all 
 select 3,3,88 union all 
 select 3,4,56 union all 
 select 4,2,88 union all 
 select 4,3,90 union all 
 select 4,4,93 union all 
 select 5,1,46 union all 
 select 5,3,78 union all 
 select 5,4,53 union all 
 select 6,1,35 union all 
 select 6,2,68 union all 
 select 6,4,71;

4 Frequently asked questions

(1) Query the student ID of all students whose ID is "001" and whose score is higher than "002";

	 select a.s_id FROM
	 (select s_id,score from student_course where c_id = '001') a,(select s_id,score from student_course where c_id = '002') b
	 where a.score > b.score and a.s_id = b.s_id;
| s_id |
+------+
|    3 |

(2) Query the student number and average score with an average score greater than 60;

select s_id,avg(score)
	  from student_course
    group by s_id
    having avg(score)>60;
| s_id | avg(score) |
+------+------------+
|    1 |    64.7500 |
|    2 |    80.0000 |
|    3 |    70.5000 |
|    4 |    90.3333 |

(3) Query the student ID, name, number of courses and total score of all students;

select s.s_id,s.sname, count(sc.c_id),sum(sc.score) from 
student s left join student_course sc on s.s_id = sc.s_id 
group by s.s_id, s.sname;
| s_id | sname  | count(student_course.c_id) | sum(score) |
+------+--------+----------------------------+------------+
|    1 | 刘一   |                          4 |        259 |
|    2 | 钱二   |                          4 |        320 |
|    3 | 张三   |                          4 |        282 |
|    4 | 李四   |                          3 |        271 |
|    5 | 王五   |                          3 |        177 |
|    6 | 赵六   |                          3 |        174 |

(4) Query the number of teachers surnamed "Ye";

select count(*) from teacher where tname like '叶%';
| count(*) |
+----------+
|        1 |

(5) Query the student number and name of the students who have not studied the "Ye Ping" teacher's course;

 select s.s_id, s.sname from student s where s.s_id 
 not in (select sc.s_id from student_course sc, course c, teacher t 
 where sc.c_id = c.c_id and c.t_id=t.t_id and  t.tname='叶平');
| s_id | sname  |
+------+--------+
|    4 | 李四   |

(6) Query the student ID and name of the students who have studied "001" and also studied the course number "002";

select s.s_id, s.sname from student s,student_course sc where 
sc.s_id = s.s_id and sc.c_id = '001' 
and exists (select * from student s1, student_course sc1
 where s1.s_id=sc1.s_id and sc1.c_id='002' );
| s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    2 | 钱二   |
|    3 | 张三   |
|    6 | 赵六

Note:
The subquery corresponding to exists does not actually return any data, but returns True or False. EXISTS specifies a subquery, mainly used to detect the existence of rows.

(7) Query the student number and name of the students who have studied all the courses taught by teacher "Ye Ping";

 select s.s_id,s.sname from student s 
 where s.s_id in (select sc.s_id from student_course sc,course c, teacher t 
 where sc.c_id = c.c_id and c.t_id = t.t_id and t.tname ='叶平');
| s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    2 | 钱二   |
|    3 | 张三   |
|    5 | 王五   |
|    6 | 赵六   |

(8) Query the student ID and name of all students whose grades of course number "002" are lower than those of course number "001";

select a.s_id,a.sname from 
(select sc.s_id,sc.score,s.sname from student_course sc,student s 
where sc.s_id=s.s_id and sc.c_id='001')a, 
(select sc.s_id,sc.score from student_course sc,student s 
where sc.s_id=s.s_id and sc.c_id='002') b 
where a.s_id=b.s_id and a.score>b.score;
| s_id | sname  |
+------+--------+
|    3 | 张三   |
+------+--------+

(9) Query the student ID and name of all students whose course scores are less than 60 points;

select s.s_id,s.sname from student s 
where s.s_id in (select sc.s_id from student_course sc 
where sc.score<60);
 s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    3 | 张三   |
|    5 | 王五   |
|    6 | 赵六   |

(10) Query the student ID and name of students who have not studied all courses;

 select s.s_id,s.sname from student s, student_course sc 
 where sc.s_id = s.s_id group by s.s_id,s.sname 
 having count(sc.c_id)<(select count(c_id) from course);
| s_id | sname  |
+------+--------+
|    4 | 李四   |
|    5 | 王五   |
|    6 | 赵六   |

note:
Aggregate functions such as count cannot be used in the where clause. The role of the where clause is to filter data before grouping, and having to filter data after grouping. The conditions often include aggregate functions.

(11) Query the student ID and name of at least one class that is the same as the student whose student ID is "1";

select distinct s.s_id,s.sname from student s,student_course sc 
where s.s_id=sc.s_id and sc.c_id in 
(select c.c_id from student s, course c, student_course sc
 where s.s_id = sc.s_id and s.s_id = '1');
| s_id | sname  |
+------+--------+
|    1 | 刘一   |
|    2 | 钱二   |
|    3 | 张三   |
|    4 | 李四   |
|    5 | 王五   |
|    6 | 赵六   |

(12) Arrange the last two letters of sname in the student table in ascending order

select sname from student order by substr(sname,length(sname)-1,2);

Usage of substr:

substr(string string,num start,num length);

string为字符串;

start为起始位置;

length为长度。

Reference article: http://www.manongjc.com/article/41690.html

Guess you like

Origin blog.csdn.net/glpghz/article/details/108304216