Multi-table queries practice --- job

- 1. Query greater than 60 points all students name and student number (DISTINCT: de-emphasis)

mysql> select student.sname,score.number from student left join score on student_id = student.sid where score.number > 60;
+-------+--------+
| sname | number |
+-------+--------+
| 铁锤  |    100 |
+-------+--------+
1 row in set (0.00 sec)

- 2. Query the number of courses taught per teacher and teacher information

mysql> select teacher.tname,count(tearch_id) from teacher right join course on tearch_id = teacher.tid  group by tearch_id;
+-------+------------------+
| tname | count(tearch_id) |
+-------+------------------+
| 波多  |                2 |
| 苍空  |                1 |
+-------+------------------+
2 rows in set (0.00 sec)

- 3. Query class student information and student information is located

mysql> select student.sname, class.caption from student left join class on class_id = class.cid;
+-------+----------+
| sname | caption  |
+-------+----------+
| 钢蛋  | 三年二班 |
| 铁锤  | 三年二班 |
| 山炮  | 一年一班 |
+-------+----------+
3 rows in set (0.00 sec)

- the number of students in the number of boys and girls 4

mysql> select gender,count(gender) from student group by gender;
+--------+---------------+
| gender | count(gender) |
+--------+---------------+
| 男     |             1 |
| 女     |             2 |
+--------+---------------+
2 rows in set (0.00 sec)

--5, get all the learning 'biological' number of students learning and achievement; Name

mysql> select student.sid,student.sname,score.number from score
    -> left join student on score.student_id = student.sid
    -> left join course on score.corse_id = course.cid
    -> where course.cname = '生物';
+------+-------+--------+
| sid  | sname | number |
+------+-------+--------+
|    1 | 钢蛋  |     60 |
+------+-------+--------+
1 row in set (0.00 sec)

- 6, the query is greater than the average score of 60 points and the number of students in school grade point average;

mysql>  select student.sid,avg(number) from student left join score on student_id = student.sid
    ->  group by student.sid
    ->  having avg(number) > 60;
+-----+-------------+
| sid | avg(number) |
+-----+-------------+
|   2 |    100.0000 |
+-----+-------------+
1 row in set (0.00 sec)

--7 query teacher surnamed "Li" of the number;

mysql> select teacher.tname ,count(tname) from teacher group by tname;
+-------+--------------+
| tname | count(tname) |
+-------+--------------+
| 波多  |            1 |
| 苍空  |            1 |
| 饭岛  |            1 |
+-------+--------------+
3 rows in set (0.00 sec)

- 8, 60 points less than the query course grade students of school, name;

mysql> select student.sid,student.sname from student left join score on student_id
= student.sid where score.number < 60;
+-----+-------+
| sid | sname |
+-----+-------+
|   1 | 钢蛋  |
+-----+-------+
1 row in set (0.00 sec)

- 9. Delete learning "flat leaf" class teacher SC table records

- 10. Query subjects the highest score and lowest points: Displayed in the following form: Course ID, highest score, lowest score;

mysql> select course.cid,max(number),min(number) from score
    -> left join course on score.corse_id = course.cid
    -> group by corse_id;
+------+-------------+-------------+
| cid  | max(number) | min(number) |
+------+-------------+-------------+
|    1 |          60 |          60 |
|    2 |         100 |          59 |
+------+-------------+-------------+
2 rows in set (0.00 sec)

- 11 queries per course is elective students

mysql> select score.corse_id, count(corse_id) from score group by corse_id;
+----------+-----------------+
| corse_id | count(corse_id) |
+----------+-----------------+
|        1 |               1 |
|        2 |               2 |
+----------+-----------------+
2 rows in set (0.00 sec)

- 12. Query surname "Chang" in the list of students;

mysql> select * from student where sname like '山%';
+-----+-------+--------+----------+
| sid | sname | gender | class_id |
+-----+-------+--------+----------+
|   3 | 山炮  | 男     |        2 |
+-----+-------+--------+----------+
1 row in set (0.00 sec)

- 13. The average scores for each query of course, results in ascending order according to the average score, average score of the same, according to the course number in descending order

mysql> select corse_id, avg(number) from  score group by corse_id order by avg(number) asc,corse_id desc;
+----------+-------------+
| corse_id | avg(number) |
+----------+-------------+
|        1 |     60.0000 |
|        2 |     79.5000 |
+----------+-------------+
2 rows in set (0.00 sec)

- 14. Query all students grade point average of more than 85 school number, name and grade point average

mysql> select student.sid,student.sname, avg(number) from student left join score on student_id = student.sid group by student.sid having avg(number) > 85;
+-----+-------+-------------+
| sid | sname | avg(number) |
+-----+-------+-------------+
|   2 | 铁锤  |    100.0000 |
+-----+-------+-------------+
1 row in set (0.00 sec)

- No. 15. The details of the curriculum and course grade 3 students learn numbers and names of 80 points or more;

- 16. For inquiries each elective courses and the corresponding number of

mysql> select cid,count(corse_id) from course left join score on corse_id = course.cid group by cid;
+-----+-------------------+
| cid | count(student_id) |
+-----+-------------------+
|   1 |                 1 |
|   2 |                 2 |
|   3 |                 0 |
+-----+-------------------+
3 rows in set (0.00 sec)

- 17. Query "4" course score less than 60, students learn numbers in descending order by score

- 18. delete student number to score a "1" course students of "2"

answer

- All new table

# 班级表(class)
mysql> create table class(
    -> cid int auto_increment primary key,
    -> caption varchar(32) not null default ' '
    -> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)

mysql> insert into clas(caption) values('三年二班'),('一年一班'),('三年一班');
ERROR 1146 (42S02): Table 'day34.clas' doesn't exist
mysql> insert into class(caption) values('三年二班'),('一年一班'),('三年一班');
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from class;
+-----+----------+
| cid | caption  |
+-----+----------+
|   1 | 三年二班 |
|   2 | 一年一班 |
|   3 | 三年一班 |
+-----+----------+
3 rows in set (0.00 sec)


# 老师表(teacher)
mysql> create table teacher(
    -> tid int auto_increment primary key,
    -> tname varchar(32) not null default ' '
    -> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)

mysql> insert into teacher(tname) values('波多'),('苍空'),('饭岛';
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from teacher;
+-----+-------+
| tid | tname |
+-----+-------+
|   1 | 波多  |
|   2 | 苍空  |
|   3 | 饭岛  |
+-----+-------+
3 rows in set (0.00 sec)
                                                       
                                                       
# 学生表(student)
mysql> create table student(
    -> sid int auto_increment primary key,
    -> sname varchar(32) not null default ' ',
    -> gender enum('男','女'),
    -> class_id int,         ,
    -> constraint student_class foreign key(class_id) references class(cid)
    -> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)
                                                       
mysql> insert into student(sname,gender,class_id) values ('钢蛋','女',1),('铁锤n','女',1),('山炮','男',2);

mysql> select * from student;
+-----+-------+--------+----------+
| sid | sname | gender | class_id |
+-----+-------+--------+----------+
|   1 | 钢蛋  | 女     |        1 |
|   2 | 铁锤  | 女     |        1 |
|   3 | 山炮  | 男     |        2 |
+-----+-------+--------+----------+
3 rows in set (0.00 sec)
                                                       
                                                       
# 课程表(course)
mysql> create table course(
    -> cid int auto_increment primary key,
    -> cname varchar(32) not null default ' ',
    -> tearch_id int,
    -> constraint teacher_course foreign key(tearch_id) references teacher(tid)
    -> )charset=utf8;
Query OK, 0 rows affected (0.04 sec)
                                                       
mysql> insert into course(cname,tearch_id) values ('生物',1),('体育',1),('物理',2;
Query OK, 3 rows affected (0.00 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from course;
+-----+-------+-----------+
| cid | cname | tearch_id |
+-----+-------+-----------+
|   1 | 生物  |         1 |
|   2 | 体育  |         1 |
|   3 | 物理  |         2 |
+-----+-------+-----------+
3 rows in set (0.00 sec)
                                                                     

# 成绩表(score)
mysql>  create table score(
    ->  sid int auto_increment primary key,
    ->  student_id int,
    ->  corse_id int,
    ->  number int,
    ->  constraint score_student foreign key(student_id) references student(sid),
    ->  constraint score_course foreign key(corse_id) references course(cid)
    -> )charset=utf8;
Query OK, 0 rows affected (0.14 sec)

mysql> insert into score(student_id,corse_id,number) values (1,1,60),(1,2,59),(2,2,100);
Query OK, 3 rows affected (0.01 sec)
Records: 3  Duplicates: 0  Warnings: 0

mysql> select * from score;
+-----+------------+----------+--------+
| sid | student_id | corse_id | number |
+-----+------------+----------+--------+
|   1 |          1 |        1 |     60 |
|   2 |          1 |        2 |     59 |
|   3 |          2 |        2 |    100 |
+-----+------------+----------+--------+
3 rows in set (0.00 sec)  

Guess you like

Origin www.cnblogs.com/whkzm/p/11769128.html