day 35 (job)

'''
create table class(
cid int auto_increment primary key,
caption char(4) not null default '尚未开班'
)charset utf8;

insert into class (caption) values ​​( 'second class three years'), ( 'three times a year'), ( 'three-class');

create table student(
sid int auto_increment primary key,
sname char(2) not null default 'sb',
gender enum('male','female') default 'female',
class_id int not null default 1,
constraint fk_st_cl foreign key (class_id) references class(cid)
)charset utf8;

Student INTO INSERT (sname, Gender, class_id) values
( 'steel egg', 'FEMALE',. 1),
( 'hammer', 'FEMALE',. 1),
( 'funk', 'MALE',. 1),
( 'John Doe', 'FEMALE',. 1),
( 'John Doe', 'male', 1),
( 'Cai', 'FEMALE',. 1),
( 'Zhang Qing', 'male', 1) ,
( 'two sheets', 'MALE',. 1),
( 'Liu', 'FEMALE', 2),
( 'Tetsuji', 'FEMALE',. 3),
( 'pheasants', 'MALE', 3),
( "Zhang Kai ',' FEMALE ', 2),
(' Mei ',' male ', 2),
(' Wang Zi ',' FEMALE ', 3),
(' Liu ',' male ', 3),
(' Ergou ',' male ', 2) ;

create table teacher(
tid int auto_increment primary key,
tname char(2) not null default 'sb'
)charset utf8;

Teacher INTO INSERT (tname) values
( 'flat leaf'), ( 'multi-wave'), ( 'empty Cang'), ( 'Iijima'), ( 'Ozawa'), ( 'Li'), ( ' Li a '), (' two Li ');

create table course (
cid int auto_increment primary key,
cname char(3) not null default '神精学',
teacher_id int not null default 3,
constraint fk_co_te foreign key (teacher_id) references teacher(tid)
)charset utf8;

Course INTO INSERT (CNAME, teacher_id) values
( 'biology', 1), ( 'physics class', 2),
( 'PE', 2), ( 'English classes', 3),
( 'Japanese lessons' , 4), ( 'language lessons', 1),
( 'chemistry class', 1), ( 'geography lesson', 5),
( 'psychology class', 4), ( 'physique class', 5),
( 'music lessons', 3), ( 'yoga class', 2);

create table score(
sid int auto_increment primary key,
student_id int not null default 1,
course_id int not null default 5,
constraint fk_st_id foreign key (student_id) references student(sid),
constraint fk_co_id foreign key (course_id) references course(cid)
)charset utf8;

alter table score
add number int not null default 0;

insert into score(student_id,course_id,number) values
(1,2,90),(1,4,35),(2,9,65),(3,4,55),
(3,8,96),(5,6,78),(1,2,65),(16,10,25),
(4,12,100),(11,11,55),(6,6,99),(4,3,55),
(5,3,59),(13,9,60),(5,6,100),(6,10,100),
(6,1,24),(14,3,8),(1,5,6),(9,6,66),
(7,6,66),(15,2,69),(3,8,88),(8,7,78),
(8,8,88),(12,12,98),(9,1,65),(5,3,18),
(9,5,66),(10,6,64),(9,9,19),(1,3,96);

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

select sid,sname from student where sid in (select distinct student_id from score where number>60);

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

select teacher_id as id,tname,count(cid) as unm from
(select cid,teacher_id,tname from course left join teacher on teacher_id = teacher.tid) as ff
group by teacher_id;

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

select sid,sname,caption from student left join class on class_id = class.cid;

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

select gender,count(sid) from student group by gender;

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

select student.sid,sname,number from student right join
(select * from score where course_id = 1)as score
on student.sid = student_id;

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

select student_id as id,sname,avg_n from student right join
(select student_id,avg(number) as avg_n from score group by student_id having avg_n>60) as score
on student_id = sid;

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

select count(tid) as l_count from teacher where tname like '李%' ;

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

select sid,sname from student right join
(select distinct student_id from score where number<60) as score
on sid = student_id;

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

delete from score where course_id in
(select cid from course left join teacher on teacher_id = tid where tname='叶平');

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

select cname,number from course right join
(select max(number) as number,course_id from score group by course_id) as score
on cid = course_id;

- 11 queries per course is elective students

select cname,num_st from course left join
(select count(sid) as num_st,course_id from score group by course_id) as score
on cid = course_id;

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

select sid,sname from student where sname like '张%';

- 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

select cid,cname,avg_n from course right join
(select course_id,avg(number) as avg_n from score group by course_id ) as score
on cid = course_id order by avg_n asc,cid desc;

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

select sid,sname,avg_n from student right join
(select student_id,avg(number) as avg_n from score group by student_id having avg_n>85) as score
on sid = student_id;

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

select sid,sname from student right join
(select student_id from score where course_id =3 and number>80) as score
on sid = student_id;

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

select cname,num_st from course left join
(select count(sid) as num_st,course_id from score group by course_id) as score
on cid = course_id;

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

select sid,sname,number from student right join
(select student_id,number from score where course_id = 4 and number<60) as score
on sid = student_id order by number desc;

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

delete from score where student_id=2 and course_id=1;

'''
'''

+ -------------- + ----- +
| cid | Caption |
+ ----- + -------------- +
| 1 | three second class |
| 2 | three times a year |
| 3 | three-year intervals |
+ ----- + -------------- +

----- + ----------- + ------------ + +
| cid | CNAME | teacher_id |
+ ----- + ---- + ------------ + -------
| 1 | biology class | 1 |
| 2 | physics class | 2 |
| 3 | PE | 2 |
| 4 | English lesson | 3 |
| 5 | Japanese lessons | 4 |
| 6 | language courses | 1 |
| 7 | chemistry class | 1 |
| 8 | geography lesson | 5 |
| 9 | psychology class | 4 |
| 10 | physique class | 5 |
| 11 | music lessons | 3 |
| 12 | yoga class | 2 |
+ ----- + ----------- + ------------ +
12 rows in set (0.00 sec)

+ -------- + ----- +
| tid | tname |
+ ----- + -------- +
| 1 | Ye Ping |
| 2 | Bodo |
| 3 | Cang empty |
| 4 | Iijima |
| 5 | Ozawa |
+ ----- + -------- +
5 rows in the SET (0.00 sec)

-------- + -------- + ----- + + ---------- +
| sid | sname | Gender | class_id |
+ ---- - + -------- + -------- + ---------- +
|. 1 | steel egg | female | 1 |
| 2 | hammer | female | 1 |
|. 3 | ShanBao | male | 1 |
|. 4 | Zhang | FEMALE |. 1 |
|. 5 | Doe | male | 1 |
|. 6 | Cai | FEMALE |. 1 |
|. 7 | Zhang Qing | male | 1 |
|. 8 | two sheets | MALE |. 1 |
|. 9 | Liu | female | 2 |
| 10 | Tetsuji | FEMALE |. 3 |
|. 11 | pheasant | MALE |. 3 |
| 12 is | Zhang Kai | female | 2 |
| 13 is | Mei | MALE | 2 |
| 14 | Wang Zi | FEMALE |. 3 |
| 15 | Liu | MALE |. 3 |
| 16 | Ergou | MALE | 2 |
+ ----- + --- + -------- + ---------- + -----
16 rows in the SET (0.00 sec)

+-----+------------+-----------+--------+
| sid | student_id | course_id | number |
+-----+------------+-----------+--------+
| 1 | 1 | 2 | 90 |
| 2 | 1 | 3 | 35 |
| 3 | 2 | 9 | 65 |
| 4 | 3 | 8 | 65 |
| 5 | 3 | 8 | 96 |
| 6 | 5 | 6 | 78 |
| 7 | 1 | 2 | 65 |
| 8 | 16 | 10 | 25 |
| 9 | 4 | 12 | 100 |
| 10 | 11 | 11 | 55 |
| 11 | 6 | 6 | 99 |
| 12 | 4 | 3 | 55 |
| 13 | 5 | 3 | 59 |
| 14 | 13 | 9 | 60 |
| 15 | 5 | 6 | 100 |
| 16 | 6 | 10 | 100 |
| 17 | 6 | 1 | 24 |
| 18 | 14 | 3 | 8 |
| 19 | 1 | 5 | 6 |
| 20 | 9 | 6 | 66 |
| 21 | 7 | 6 | 66 |
| 22 | 15 | 2 | 69 |
| 23 | 3 | 8 | 88 |
| 24 | 8 | 7 | 78 |
| 25 | 8 | 8 | 88 |
| 26 | 12 | 12 | 98 |
| 27 | 9 | 1 | 65 |
| 28 | 5 | 3 | 18 |
| 29 | 9 | 5 | 66 |
| 30 | 10 | 6 | 64 |
| 31 | 9 | 9 | 19 |
| 32 | 1 | 3 | 32 |
+-----+------------+-----------+--------+
32 rows in set (0.00 sec)

'''

Guess you like

Origin www.cnblogs.com/luocongyu/p/11769161.html
Recommended