我已經記不起來,有多久沒更新文章了。
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公里,加油!
優秀不夠,你是否無可替代
軟體測試交流QQ群:721256703,期待你的加入!!
歡迎關注我的微信公眾號:軟體測試君