11.21面試50題第一題-今天也是努力學習的小楊同學
第一題的題目為:查詢"01"課程比"02"課程成績高的學生的資訊及課程分數
應用的資料如下:
course表:
score表:
student表:
teacher表:
解答程序:
1.四個表格之間的聯系如下

對于第一題來說我們需要的邏輯線就是
student.s_id-score.s_id-course.c_id
按照小楊的構思習慣,結果導向想要什么就select 什么 ,將題目拆分
1.查詢01課程中學生的資訊以及課程分數
即為:select s_id,c_id,s_score from score

2.查詢02課程中學生的資訊以及課程分數
即為:select s_id,c_id,s_score from score

那么下一步的思路就是將這兩個表作為子查詢進行聯結,同時題目的要求是要查詢學生的資訊及課程分數
即為:select s_id,s_name,01課程分數,02課程的分數 from 建立的子查詢 where 01課程分數>02課程分數;
整體的思路就是這樣子,以下是具體的代碼:
select s_id,s_name,a.s_score as ‘01’,b.s_score’02’ from
**第一個子查詢(select s_id,c_id,s_score from score where c_id=‘’01‘’) as a
**第二個子查詢為(select s_id,c_id,s_score from score where c_id=02‘’) as b
將兩個子查詢進行聯結關鍵字為:inner join
(select s_id,c_id,s_score from score where c_id=‘01’)as a
INNER JOIN
(select s_id,c_id,s_score from score where c_id=‘02’)as b
on a.s_id=b.s_id
子查詢建立好了之后就將我們需要的資訊進行整合為:
select a.s_id,a.s_score as ‘01’,b.s_score as ‘02’ from(
(select s_id,c_id,s_score from score where c_id=‘01’)as a
INNER JOIN
(select s_id,c_id,s_score from score where c_id=‘02’)as b
on a.s_id=b.s_id
)
如圖:
到這我們發現一個問題
就是這上面只有學生的id并沒有學生的名字資訊,
那么我們需要再在建立一個查詢學生名字的子查詢,那么聯結的就是學生表,因為只有學生表中有學生的編碼以及名字,
(select s_id,s_name from student)as s on s.s_id=b.s_id)
where a.s_score>b.s_score
同樣適用inner join聯結
整合為:select a.s_id,s.s_name,a.s_score as ‘01’,b.s_score as ‘02’ from(
(select s_id,c_id,s_score from score where c_id=‘01’)as a
INNER JOIN
(select s_id,c_id,s_score from score where c_id=‘02’)as b
on a.s_id=b.s_id
INNER JOIN
(select s_id,s_name from student)as s on s.s_id=b.s_id)
where a.s_score>b.s_score;
結果為:

啊這美好的一天
ps:記錄自己學習的程序,別來上綱上線奧,否則小心順著你的網線咬死你!
如果有一起學習的小伙伴可以私我呀!!一起努力!!!
轉載請註明出處,本文鏈接:https://www.uj5u.com/ruanti/226918.html
標籤:其他
上一篇:[STL]空間配置器的原理
下一篇:Java集合總結大全--史上最強
