簡單的SQL語句學習資料 經典SQL語句

來源:互聯網
上載者:User
簡單的SQL語句學習資料 經典SQL語句

  為了大家更容易理解我舉出的SQL語句,本文假定已經建立了一個學產生績管理資料庫,全文均以學產生績的管理為例來描述。   1.在查詢結果中顯示列名:  a.用as關鍵字:select name as '姓名' from students order by age  b.直接表示:select name '姓名' from students order by age

  2.精確尋找:  a.用in限定範圍:select * from students where native in ('湖南', '四川')   b.between...and:select * from students where age between 20 and 30  c.“=”:select * from students where name = '李山'   d.like:select * from students where name like '李%' (注意查詢條件中有“%”,則說明是部分匹配,而且還有先後資訊在裡面,即尋找以“李”開頭的匹配項。所以若查詢有“李”的所有對象,應該命令:'%李%';若是第二個字為李,則應為'_李%'或'_李'或'_李_'。)  e.[]匹配檢查符:select * from courses where cno like '[AC]%' (表示或的關係,與"in(...)"類似,而且"[]"可以表示範圍,如:select * from courses where cno like '[A-C]%')

  3.對於時間類型變數的處理  a.smalldatetime:直接按照字串處理的方式進行處理,例如:
select * from students where birth > = '1980-1-1' and birth <= '1980-12-31'

  4.集合函式  a.count()求和,如:select count(*) from students (求學生總人數)  b.avg(列)求平均,如:select avg(mark) from grades where cno=’B2’  c.max(列)和min(列),求最大與最小

  5.分組group  常用於統計時,如分組查總數:  select gender,count(sno)   from students  group by gender  (查看男女學生各有多少)  注意:從哪種角度分組就從哪列"group by"  對於多重分組,只需將分組規則羅列。比如查詢各屆各專業的男女同學人數 ,那麼分組規則有:屆別(grade)、專業(mno)和性別(gender),所以有"group by grade, mno, gender"  select grade, mno, gender, count(*)  from students  group by grade, mno, gender  通常group還和having聯用,比如查詢1門課以上不及格的學生,則按學號(sno)分類有:  select sno,count(*) from grades   where mark<60  group by sno  having count(*)>1

  6.UNION聯合  合并查詢結果,如:  SELECT * FROM students  WHERE name like '張%’  UNION [ALL]  SELECT * FROM students  WHERE name like '李%’

  7.多表查詢  a.內串連  select g.sno,s.name,c.coursename   from grades g JOIN students s ON g.sno=s.sno  JOIN courses c ON g.cno=c.cno  (注意可以引用別名)  b.外串連  b1.左串連  select courses.cno,max(coursename),count(sno)   from courses LEFT JOIN grades ON courses.cno=grades.cno   group by courses.cno  左串連特點:顯示全部左邊表中的所有項目,即使其中有些項中的資料未填寫完全。  左外串連返回那些存在於左表而右表中卻沒有的行,再加上內串連的行。  b2.右串連  與左串連類似  b3.全串連  select sno,name,major   from students FULL JOIN majors ON students.mno=majors.mno  兩邊表中的內容全部顯示  c.自身串連  select c1.cno,c1.coursename,c1.pno,c2.coursename   from courses c1,courses c2 where c1.pno=c2.cno  採用別名解決問題。  d.交叉串連  select lastname+firstname from lastname CROSS JOIN firstanme  相當於做笛卡兒積

   8.巢狀查詢  a.用關鍵字IN,如查詢李山的同鄉:  select * from students  where native in (select native from students where name=’ 李山’)   b.使用關鍵字EXIST,比如,下面兩句是等價的:  select * from students  where sno in (select sno from grades where cno=’B2’)  select * from students where exists   (select * from grades where   grades.sno=students.sno AND cno=’B2’)

  9.關於排序order  a.對於排序order,有兩種方法:asc升序和desc降序  b.對於排序order,可以按照查詢條件中的某項排列,而且這項可用數字表示,如:  select sno,count(*) ,avg(mark) from grades   group by sno  having avg(mark)>85  order by 3

  10.其他  a.對於有空格的識別名稱,應該用"[]"括住。  b.對於某列中沒有資料的特定查詢可以用null判斷,如select sno,courseno from grades where mark IS NULL  c.注意區分在巢狀查詢中使用的any與all的區別,any相當於邏輯運算“||”而all則相當於邏輯運算“&&”  d.注意在做否定意義的查詢是小心進入陷阱:  如,沒有選修'B2’課程的學生 :  select students.*  from students, grades  where students.sno=grades.sno  AND grades.cno <> ’B2’   上面的查詢方式是錯誤的,正確方式見下方:  select * from students  where not exists (select * from grades   where grades.sno=students.sno AND cno='B2')

  11.關於有難度多重巢狀查詢的解決思想:  如,選修了全部課程的學生:  select *  from students  where not exists ( select *  from courses   where NOT EXISTS   (select *  from grades  where sno=students.sno  AND cno=courses.cno))

 

轉自:http://www.360doc.com/content/09/1213/23/114824_11058102.shtml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.