SQL的簡(jiǎn)單查詢實(shí)例教程_第1頁
SQL的簡(jiǎn)單查詢實(shí)例教程_第2頁
SQL的簡(jiǎn)單查詢實(shí)例教程_第3頁
SQL的簡(jiǎn)單查詢實(shí)例教程_第4頁
SQL的簡(jiǎn)單查詢實(shí)例教程_第5頁
免費(fèi)預(yù)覽已結(jié)束,剩余1頁可下載查看

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、SQL的簡(jiǎn)單查詢實(shí)例教程 關(guān)鍵詞:菜鳥學(xué)數(shù)據(jù)庫之簡(jiǎn)單 SQL語句小結(jié) 為了大家更容易 理解我舉出的SQL語句,本文假定已經(jīng)建立了 一個(gè)學(xué)生成績(jī)管理數(shù)據(jù) 庫,全文均以學(xué)生成績(jī)的管理為例來描述。 1. 在查詢結(jié)果 中顯示列名: a. 用 as 關(guān)鍵字: select name as 姓名from students order by age b. 直接表示 :select name 姓名from studentsorder by age 2. 精確查找: a. 用 in 限定范圍:select * from students where native in ( 湖南,四川) b. between.a

2、nd: select * from students where age between 20 and 30 c. “ =” select * from students where name =李山 d. like:select * fromstudentswhere name like 李%(注意查詢條件中有“, 則說明是部分匹配,而且還有先后信息在里面,即查找以李”開頭的匹配項(xiàng)。所以若查詢 有 李”的所有對(duì)象,應(yīng)該命令:李:若是第二個(gè)字 為李,則應(yīng)為_李%或_李或_李 _。) e. 匹配檢查符:select * from courses 與in(.)類似,而且可以表示范圍,如: A-C%

3、) where cno like AC%(表示或的關(guān)系, select * from courses where cno like 3. 對(duì)于時(shí)間類型變量的處理 a. smalldatetime :直接按照字符串處理的方式 進(jìn)行處理,例如: 1980-12-31 select * from students where birth =1980-1-1 and birth = 4. 集函數(shù) a. count() 求和,如:select count(*)from students(求學(xué)生總?cè)藬?shù)) b. avg( 歹U)求平均,如: select avg(mark) from grades wher

4、e cno= B2 c. max(列)和min(列),求最大與最小 5. 分組 group 常用于統(tǒng)計(jì)時(shí),如分組查總數(shù): select gender,count(sno) from students group by gender (查看男女學(xué)生各有多少) 注意:從哪種角度分組就從哪列g(shù)roup by 對(duì)于多重分組,只需將分組規(guī)則羅列。比如查詢各屆各專業(yè)的男女同學(xué)人數(shù),那么分 組規(guī)則有:屆別(grade)、專業(yè)(mno)和性別(gender),所以有g(shù)roup by grade, mno, gen der select grade, mno, gender, count(*) from stu

5、dents group by grade, mno, gender 通常group還和having 聯(lián)用,比如查詢1門課以上不及格的學(xué)生,則按學(xué)號(hào)(sno) 分類有: select sno ,co un t(*)from grades where mark1 6. UNION 聯(lián)合 合并查詢結(jié)果,女口: SELECT * FROM students WHERE name like 張 UNION ALL SELECT * FROM students WHERE name like 李 7. 多表查詢 a.內(nèi)連接 select g.sno,,c.coursename from gra

6、des g JOIN students s ON g.sno=s.sno JOIN courses c ONo=o (注意可以引用別名) b. 外連接 b1.左連接 select o,max(coursename),count(sno) from courses LEFT JOIN grades ON o=o group by o 左連接特點(diǎn):顯 示全部左邊表中的所有項(xiàng)目,即使其中有些項(xiàng)中的數(shù)據(jù)未填寫完全。 左外連接返回那 些存在于左表而右表中卻沒有的行,再加上內(nèi)連接的行。 b2.右連接 與左連接類似 b3.全連接 select sno,name,major from students FUL

7、L JOIN majors ONstudents.mno=majors.mno 兩邊表中的內(nèi)容 全部顯示 c. 自身連接 select c1.c no ,c1.course name,c1.p no ,c2.course name from courses c1,courses c2 where c1.pno=o 采用別名解決問題。 d. 交叉連接 select last name+first namefrom last name CROSS JOIN firsta nme 相當(dāng)于做笛卡兒積 8. 嵌套查詢 a. 用關(guān)鍵字IN,如查詢李山的同鄉(xiāng): select * from stude nts

8、 where native in (select native fromstudents where name= 李山) b. 使用關(guān)鍵字EXIST,比如,下面兩句是等價(jià)的: select * from stude nts where sno in (select sno from grades where cno= B2) select * from stude nts where exists (select * from grades where grades.s no=stude nts.s no AND cno= B2 ) 9. 關(guān)于排序order a.對(duì)于排序order ,有兩種方

9、法:asc升序和desc降序 b.對(duì)于排序order,可以按照查詢條件中的某項(xiàng) 排列,而且這項(xiàng)可用數(shù)字表示,如: select sno ,co un t(*),avg(mark) from grades group by sno hav ing avg(mark)85 order by 3 10. 其他 a. 對(duì)于有空格的識(shí)別名稱,應(yīng)該用括住。 b. 對(duì)于某列中沒有數(shù)據(jù)的特定查詢可以用null判斷,如select snocoursenofrom grades where mark IS NULL c. 注意區(qū)分在嵌套查詢中使用的any與all的區(qū)別,any相當(dāng)于邏輯運(yùn)算“而all則 相當(dāng)于邏輯

10、運(yùn)算& ” d. 注意在做 否定意義的查詢是小心進(jìn)入陷阱: 女口,沒有選修B2 課程的學(xué)生: select students.* from students, grades where students.sno=grades.sno AND o B2 上面的查詢方式 是錯(cuò)誤的,正確方式見下方: select * from stude nts where not exists (select * from grades where grades.sno=students.snoAND eno=B2) 11. 關(guān)于有難度多重嵌套查詢 的解決思想: 女口,選修了全部 課程的學(xué)生: select * from students where not exists ( select * from

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

評(píng)論

0/150

提交評(píng)論