sql查詢語句select 應用舉例

數據表的查詢(select)

  select 字段列表 [as 別名], * from 數據表名

  [where 條件語句]

  [group by 分組字段]

  [order by 排序字段列表 desc]

  [LIMIT startrow,rownumber]

  1、Select 字段列表 From 數據表

  例:①、select id,gsmc,add,tel from haf (* 表示數據表中所有字段)

  ②、select 單價,數量,單價*數量 as 合計金額 from haf (As 設置字段的別名)

  2、Select … from … Where 篩選條件式

  篩選條件式:①、字符串數據: select * from 成績單 Where 姓名='李明'

  ②、萬用字符:  select * from 成績單 Where 姓名 like '李%'

  select * from 成績單 Where 姓名 like '%李%'

  select * from 成績單 Where 姓名 like '%李_'

  ③、特殊的條件式:

  ⑴= / > / < / <> / >= / <=

  ⑵AND(邏輯與) OR(邏輯或) NOT(邏輯非)

  ⑶Where 字段名稱 in(值一,值二)

  ⑷Where 字段名稱 Is Null / Where 字段名稱 Is Not Null

  3、Select … from … group by 字段

  SQL函數:

  SELECT sex,count(id) as women from `user` group by 'sex';

  函數名描述函數名描述

  AVG平均值Count計數

  MAX最大值MIN最小值

  Sum求和

  4、Select … from … Order by 字段列表 desc(倒,如果直接寫爲順序)

  5、Select … from … LIMIT ".$start_rowno.",".($pagesize+1)

  第二節 SQL語句實例應用

  數據庫說明:

  student(學生表):

  stdid int(11) id號

  son char(5) 學號

  sname char(20) 姓名

  ssex tinyint(1) 性別

  sage char(3) 年齡

  sdept char(20) 所在系

  course(課程表):

  couid int(11) id號

  cno char(5) 課程號

  cname char(20) 課程名

  cpno char(6) 選修課號

  ccredit char(50) 學分

  sc(學生選課表):

  scid int(11) id號

  cno char(5) 課程號

  grade float 成績

  sno char(5) 學號

  單表查詢:

  一、選擇表中的若干字段:

  查詢指定列:

  1、查詢全體學生的學號與姓名;

  select son,sname from student

  2、查詢全體學生的姓名、學號、所在系;

  select sname,son,sdept from student

  3、查詢全體學生的詳細記錄;

  select * from student

  查詢經過計算的值:

  4、查全體學生的姓名及其出生年份

  select sname,year(now())-sage as '出生年份' from student

  5、查詢全體學生的姓名、出生年份和所有系,要求用大(小)寫字母表示所有系名

  select sname as '姓名','出生與',year(now())-sage as '出生年份',UPPER(sdept) as '系別' from student

  select sname as '姓名','出生與',year(now())-sage as '出生年份',lower(sdept) as '系別' from student

  二、選擇表中的若干記錄:

  消除取值重複的行:

  6、查詢選修了課程的學生學號

  select distinct sno from sc

  查詢滿足條件的記錄:

  比較大小:

  7、查詢計算機全體學生的名單

  select sname from student where sdept='cs'

  8、查詢所有年齡在20歲以下的學生姓名及其年齡

  select sname,sage from student where sage<20

  9、查詢考試成績小於90分的學生的學號

  select distinct sno from sc where grade<90

  確定範圍:

  10、查詢年齡在18-20歲之間的學生的姓名、系別和年齡。

  select sname,sdept,sage from student where sage between 18 and 20

  11、查詢年齡不在19-20歲之間的學生的姓名、系別和年齡。

  select sname,sdept,sage from student where sage not between 19 and 20

  確定集合:

  12、查詢信息系(is)、數學系(ma)和計算機科學系(cs)學生的姓名和性別。

  select sname,ssex from student where sdept in('is','ma','cs')

  13、查詢不是信息系(is)、數學系(ma)的學生的姓名、系別和年齡。

  select sname,ssex from student where sdept not in('is','ma')

  字符匹配(like '<匹配串>' %代表任意長度(長度可以爲0)的字符串 ; _代表任意單個字符,漢字得用兩個"__"):

  14、查詢學號爲95001的學生的詳細情況

  select * from student where son like '95001'

  15、查詢所有姓名李的學生的姓名、學號和性別。

  select sname,son,ssex from student where sname like '李%'

  16、查詢姓名是兩個字學生的姓名、學號和性別。

  select sname,son,ssex from student where sname like '____'

  17、查詢所有不姓李的學生姓名。

  select sname from student where sname not like '李__'

  涉及空值的查詢:

  18、某些學生選修課程後沒有參加考試,所以有選課記錄,但沒有考試成績,查詢缺少成績的學生的學號和相應的課程號。

  select sno,cno from sc where grade is null

  19、查詢所有有成績的學生學號和課程號。

  select sno,cno from sc where grade is not null

  多重條件查詢(and or):

  20、查詢計算機系年齡在20歲的學生姓名。

  select sname from student where sdept='cs' and sage=20

  21、查詢信息系(is)、數學系(ma)和計算機科學系(cs)學生的姓名和性別。

  select sname,ssex from student where sdept='is' or sdept='ma' or sdept='cs'

  三、對查詢結果排序:

  22、查詢選修了3號課程的學生的學號及其成績,查詢結果按分數的降序排列。

  select sno,grade from sc where cno='3' order by grade desc

  23、查詢全體學生情況,查詢結果按所在系的系號升序排列,同一系中的學生按年齡降序排列。

  select * from student order by sdept,sage desc

  四、使用集函數:

  24、查詢學生總人數。

  select count(*) as '總人數' from student

  25、查詢選修了課程的學生人數。

  select count(distinct sno) as '人數' from sc

  26、計算1號課程的學生平均成績

  select format(avg(grade),2) as '平均成績' from sc where cno='1'

  27、查詢選修1號課程的學生最高分數。

  select max(grade) from sc where cno='1'

  五、對查詢結果分組:

  28、求各個課程號及相應的選課人數。

  select cno as '課程號',count(sno) as '人數' from sc group by cno

  29、查詢選修了3門以上課程的學生學號。

  select sno from sc group by sno having count(*)>2

  注:where 子句與 having 短語的區別在於作用對象不同,where 子句作用於基本表或視圖,從中選擇滿足條件的記錄,having短語作用於組,從中選擇滿足條件的組。

  多表查詢

  同時查詢兩個以上的表,稱爲連接查詢。

  等值連接:當連接運算符爲=時,爲等值連接。

  1、查詢每個學生及其選修課程的情況(等值連接)。

  select student.*,sc.* from student,sc where student.son=sc.sno

  自然連接:在等值連接中把目標列中重複的屬性列去掉。

  2、查詢每個學生及其選修課程的情況(自然連接)。

  select student.son,sname,ssex,sage,sdept,cno,grade from student,sc where student.son=sc.sno

  自身連接:連接操作不僅可以在兩個表之間進行,也可以是一個表與其自己進行連接。

  3、查詢每一門課的間接先修課。

  select a.cno,b.cpno,a.cname from course a,course b where a.cpno=b.cno

  複合條件連接:

  4、查詢選修2號課程且成績在90分以上的所有學生。

  select a.son,sname from student a,sc b where a.son=b.sno and b.cno='2' and b.grade>90

  5、查詢每個學生的學號、姓名、選修的課程名及成績。

  select a.son,sname,cname,grade from student a,sc b ,course c where a.son=b.sno and b.cno=c.cno

發佈了118 篇原創文章 · 獲贊 0 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章