我已经记不起来,有多久没更新文章了。
5月中旬我还在上班,中旬以后一系列发生的事情,真的远远超出了可承受范围,只能硬着头皮面对!
我是谁,我应该是谁,又能怎样,只能向前·····
# 自链接 SELECT c.name,t.name FROM course c,teacher t WHERE c.teacher_id=t.id # 内连接 SELECT course.name,teacher.name FROM course INNER JOIN teacher ON course.teacher_id=teacher.id
结果:
# 内连接 SELECT shuju.student_id FROM (SELECT score.course_id, score.student_id, score.mark FROM score INNER JOIN course ON score.course_id=course.id WHERE course.name='数据结构') AS shuju INNER JOIN (SELECT score.course_id, score.student_id, score.mark FROM score INNER JOIN course ON score.course_id=course.id WHERE course.name='java') AS java ON shuju.student_id=java.student_id WHERE shuju.mark<java.mark # 自连接 SELECT shuju.student_id FROM (SELECT s.course_id, s.student_id, s.mark FROM score s, course c WHERE c.`name`='数据结构' AND s.course_id=c.id) shuju, (SELECT s.course_id, s.student_id, s.mark FROM score s, course c WHERE c.`name`='java' AND s.course_id=c.id) java WHERE shuju.student_id=java.student_id AND shuju.mark<java.mark
结果:
SELECT score.student_id, round(AVG(score.mark),2) AS avgScore FROM score GROUP BY score.student_id HAVING avgScore>65
结果:
SELECT student.`name`, ROUND(AVG(score.mark),2) AS avgScore FROM score INNER JOIN student ON student.id=score.student_id GROUP BY score.student_id HAVING avgScore>65
结果:
SELECT student.name AS '名字', COUNT(score.course_id) AS '选课数',SUM(score.mark) AS '总成绩'FROM score INNER JOIN student ON student.id=score.student_id GROUP BY student_id
结果:
select student.name from student where id not in(select student_id from score where course_id in(select course.id from course inner join teacher on course.teacher_id = teacher.id where teacher.name='大牛'))
结果:
select student.name from student where id in(select student_id from score where course_id in(3,3))
结果:
select student.name from student inner join score on student.id = score.student_id where score.mark<60 group by score.student_id
结果:
select student.name from student where id in (select score.student_id from score group by score.student_id having count(1)=(select count(1) from course))
结果:
SELECT student.name FROM student WHERE id IN (SELECT student_id FROM score WHERE course_id IN (SELECT course_id FROM score WHERE student_id=5)) AND student.name!='小草'
结果:
select student.name from student where id in (select student_id from score where course_id not in (select course_id from score where student_id=5)) and student.name!='小草'
结果:
select course_id as '课程id',max(mark) as '最高分',min(mark) as '最低分'from score group by course_id
结果:
# 感觉有点low,但是能查出来 select student.id as '学号',student.name as '姓名'from student inner join score on student.id = score.student_id where student.id=(select student_id from score group by student_id having count(course_id)=1) # 这个好一些 select student.id as '学号',student.name as '姓名'from student inner join score on student.id = score.student_id group by student_id having count(course_id)=1
结果:
select course.id as '课程id',course.name AS "课程名称",avg(mark) as "平均成绩" from course inner JOIN score on course.id = score.course_id group by course_id order by avg(mark) ,"平均成绩",course_id desc
结果:
按如下形式显示: 学生id、数据库原理、java语言、C语言、课程数、平均分;(高级应用较难)
select sc.student_id as '学生id', (select mark from score inner join course on course.id=score.course_id where course.name='数据库原理' and score.student_id=sc.student_id) as '数据库原理', (select mark from score inner join course on course.id=score.course_id where course.name='java' and score.student_id=sc.student_id) as 'java', (select mark from score inner join course on course.id=score.course_id where course.name='C语言' and score.student_id=sc.student_id) as 'C语言', count(course_id) as '课程数', round(avg(sc.mark),2) as '平均分' from score as sc group by sc.student_id order by avg(sc.mark) desc
结果:
整个数据库这部分的复习,早在近一个月前就开始了。
在做了两道题后,就遇到了各种事情,就被搁置了,差点被遗忘了。。。
今天有时间,接着把学习的感觉续上,总体下来,算是初步复习了下sql
的一些常用查询操作,就一个测试仔来说,我个人感觉这些都能写正确写出来,真的很厉害,我也是用了近6小时呢。
不管遇到了什么难事,学习、跑步都不能停(我又胖了5斤,好扎心).....
明天继续我的5公里,加油!