SQL高階查詢

2021-07-27 11:54:09 字數 4311 閱讀 1553

--案例1:查詢學生表中比"微冷的雨"小的學員資訊(姓名,位址)

--轉化成找出出生日期比 "微冷的雨"大的日期

select * from student

--別人要什麼,我們給什麼

select * from student

where birthday>(select birthday from student where studentno='23311')

--01.子查詢的語法是(檢索語句)

--02.子查詢優先于父查詢 (外層查詢) 預設認為沒有 32層

--03.子查詢的結果,作為父查詢的條件

--2.以後提交作業直接提交到ftp 對應資料夾

--03.pdf +++++++++c#本質論 ++++梅超風級別 via clr c#

--04.部落格

--查詢「oop」課程至少一次考試剛好等於60分的學生(姓名,成績)

select studentname,studentresult

from student,result

where student.studentno=result.studentno

and subjectid=(select top 1 subjectid from subject where subjectname='oop' order by subjectid)

and studentresult=70

--方案:內連線 3張表

select studentname,studentresult

from student,result,subject

where student.studentno=result.studentno

and subject.subjectid=result.subjectid

and subjectname='oop' --該列只在科目表subject中出現過

and studentresult=70

--請問:子查詢和表連線的使用場景以及區別 3個維度

--1.所有的表連線語句都可以用 子查詢替換 。但是子查詢語句不一定能使用表連線替換 結論:子查詢應用場景更廣

--2. where後面的子查詢 是通過查詢表中的特定條件,檢索出結果,作為條件。

-- 表連線將多表的公共列進行特定格式的組織,裝配,將整個需要的列拼出一張大表返回。

--3.

--查詢參加最近一次「oop」考試成績最高分和最低分

select max(studentresult),min(studentresult)

from result

where examdate=

( select max(examdate)

from result

where subjectid=

( select subjectid

from subject

where subjectname='oop'

))and subjectid=

( select subjectid

from subject

where subjectname='oop'

) --案例1:.查詢參加「oop」課程最近一次考試的在讀學生名單(學生姓名,學生編號)

select studentname,studentno

from student

where studentno in

( select studentno from result

where subjectid=

( select subjectid from subject

where subjectname='oop'

) and examdate=

( select max(examdate) from result

where subjectid=

(select subjectid from subject

where subjectname='oop')))

select * from student

where address='北京' or address='武漢'

select * from student

where address in('北京','武漢')

--案例1:檢查「oop」課程最近一次考試。--如果有80分以上的成績,則每人提2分;

--否則,每人提5分。最終的成績不得大於100分

if exists

( select studentresult from result

where subjectid=

(select subjectid from subject

where subjectname='oop'

)and examdate=

(select max(examdate) from result

where subjectid=

( select subjectid from subject

where subjectname='oop'

) )

and studentresult>80

) begin

--有,每人提2分 99

update result set studentresult=100

where subjectid=

(select subjectid from subject

where subjectname='oop'

)and examdate=

(select max(examdate) from result

where subjectid=

( select subjectid from subject

where subjectname='oop'

) )

and studentresult>98

update result set studentresult+=2

where subjectid=

(select subjectid from subject

where subjectname='oop'

)and examdate=

(select max(examdate) from result

where subjectid=

( select subjectid from subject

where subjectname='oop'

) )

and studentresult<=98

endelse

begin

--沒有,整體+5

update result set studentresult+=5

where subjectid=

(select subjectid from subject

where subjectname='oop'

)and examdate=

(select max(examdate) from result

where subjectid=

( select subjectid from subject

where subjectname='oop'

) )

endselect * from result

where subjectid=

(select subjectid from subject

where subjectname='oop'

)and examdate=

(select max(examdate) from result

where subjectid=

( select subjectid from subject

where subjectname='oop'

) )

---分頁:雙top 雙order by 每頁顯示3條記錄,我想要第二頁資料 4-6條

select top 5 * from student

where studentno not in

( select top 0 studentno from student

)select * from student

--預設按照主鍵列排序

select * from student

--跳過3條 取3條 2頁

--跳過6條 取3條 3頁

--跳過9條 取3條 4頁

SQL高階查詢

1建立乙個儲存過程,當工資大於8000的,工資減一成,否則加一成工資 2建立乙個函式,輸入學生學號,返回成績 student id,name,score 3查詢員工的工資,當工資大於2000時,顯示該工資 但工資低於2000,則加200元工資,然後再輸出 4求1 2 i,一直加到總和 6000,並求...

關於SQL高階查詢

關於sql高階查詢 if request btype id then sql sql btype id request btype id and if request stype id then sql sql stype id request stype id and if request tna...

sql高階語句查詢

例如 1.連線法 select a.b.from t2 mz sfdj a join t2 mz sfdjmx b on a.djid b.djid where a.ssje 5000 2.用連線來進行修改表裡記錄 update a set a.zy b.zy from t2 mz sfdj a i...