create table student(
student_id int(6) primary key auto_increment,
student_name varchar(12) not null,
student_age int(3),
student_sex varchar(4) not null
);
insert into student values(1000,'科比',16,'男'),(1001,'乔丹',17,'男'),(1002,'詹姆斯',18,'男'),(1003,'库里',14,'女'),(1004,'巴特勒',14,'男'),(1005,'乔治',21,'女'),(1006,'伦纳德',21,'男'),(1007,'汤普森',19,'男');
create table teacher(
teacher_id int(6) primary key auto_increment,
teacher_name varchar(5) not null,
teacher_sex varchar(4) not null,
teacher_age int(3)
);
insert into teacher values(1,'波波','男',30),(2,'斯波','女',31),(3,'科尔','男',33),(4,'里弗斯','女',28),(5,'少帅','女',27),(6,'小红','女',30),(7,'谢霆锋','女',45),(8,'吴京','男',41),(9,'张三','男',28),(10,'李四','男',35);
create table course(
course_id int(6) primary key auto_increment,
course_name varchar(10) not null,
teacher_id int(6)
);
insert into course values(301,'高等数学',1),(302,'线性代数',2),(303,'概率论',3),(304,'计算机基础',4),(305,'计算机原理',5),(306,'原件工程',6),(307,'python',7),(308,'java',8),(309,'c',9),(310,'c++',10),(311,'shell',1),(312,'javascript',2),(313,'单片机',3),(311,'NBA',4),(312,'mysql',5),(313,'劳动课',6);
create table score(
student_id int(6),
course_id varchar(10) not null,
student_score int(6)
);
insert into score values(1000,301,98),(1001,302,97),(1002,303,82),(1003,304,88),(1004,305,70),(1005,306,54),(1006,307,62),(1007,308,93),(1008,309,98),(1009,310,89),(1000,311,54),(1001,312,97),(1002,313,99),(1003,314,66),(1004,315,95);
select * from student;
SELECT * FROM student where student_name LIKE "巴%";
select count(*) as 男性人数 from student where student_sex="男"
select student_sex as 性别,count(*) as 总数 from student group by student_sex;
select score.course_id as 课程id,max(student_score) as 最高分,min(student_score) as 最低分
from score
group by course_id;
select course.course_id as 课程id,MAX(student_score) as 最高分,MIN(student_score) as 最低分,course_name,teacher_name
from course,teacher,score
where course.course_id=score.course_id and teacher.teacher_id=course.teacher_id
GROUP BY 课程id;
select course.course_id
from course,teacher
where course.teacher_id=teacher.teacher_id and teacher_name='斯波';
select student.student_id,student.student_name,score.course_id
from student,score
where student.student_id=score.student_id and score.course_id=(select course.course_id from course,teacher where course.teacher_id=teacher.teacher_id and teacher_name='斯波');
先是3表联查
select score.student_id
from score,teacher,course
where score.course_id=course.course_id and teacher.teacher_id=course.teacher_id
and teacher.teacher_name='波波';
select student.student_id,student.student_name
from student
where student_id in (select score.student_id
from score,teacher,course
where score.course_id=course.course_id and teacher.teacher_id=course.teacher_id
and teacher.teacher_name='波波');
select student.student_id,student.student_name
from student
where student_id not in (select score.student_id
from score,teacher,course
where score.course_id=course.course_id and teacher.teacher_id=course.teacher_id
and teacher.teacher_name='波波');
这道题最最最关键的所在,(划重点)就是:把学过编号为“01”课程的同学查询出来,再把学过编号为“02”课程的同学查询出来,然后把两张表合并到一张表中。
错误sql编写:
select student.student_id,student.student_name
from student,score
where student.student_id=score.student_id
and score.course_id=301 and score.course_id=302;
select student_id
from score
where course_id=301;
select student_id
from score
where course_id=302;
select *
from (select student_id from score where course_id=301) as a
INNER JOIN (select student_id from score where course_id=302) as b
on a.student_id=b.student_id;
select student.student_id,student.student_name
from student
where student.student_id IN(select a.student_id
from (select student_id from score where course_id=301) as a
INNER JOIN (select student_id from score where course_id=302) as b
on a.student_id=b.student_id);
select score.course_id
FROM student,score
where student.student_id=score.student_id and student.student_id=1005;
select student.student_id,student.student_name,score.course_id
FROM student,score
where student.student_id=score.student_id and score.course_id = (select score.course_id
FROM student,score
where student.student_id=score.student_id and student.student_id=1005) AND student.student_id!=1005;
SELECT course.teacher_id,course.course_name,AVG(score.student_score)
from course,score
where course.course_id=score.course_id
group by course.teacher_id
ORDER BY AVG(score.student_score) DESC;
select course.course_id
FROM course,teacher
where course.teacher_id=teacher.teacher_id and teacher.teacher_name='波波';
select student.student_name,score.student_score,score.course_id
from score,student
where student.student_id=score.student_id and score.course_id = (select course.course_id
FROM course,teacher
where course.teacher_id=teacher.teacher_id and teacher.teacher_name='波波')
ORDER BY score.student_score
DESC
LIMIT 1;
select score.course_id
from score
where score.student_id=1001;
select student.student_id,student.student_name,score.course_id
from student,score
where student.student_id=score.student_id and score.course_id in (select score.course_id
from score
where score.student_id=1001);
select score.student_score
from score
where score.student_score<=60;
select student.student_id,student.student_name,score.student_score
from student,score
where student.student_id=score.student_id and score.student_score in(select score.student_score
from score
where score.student_score<=60);
select course.course_id,course.course_name,AVG(score.student_score)
from course,score
where score.course_id=course.course_id
GROUP BY course.course_id
order by AVG(score.student_score)
desc;