| 數據表的查詢(xún)(select) select 字段列表 [as 別名], * from 數據表名 [where 條件語(yǔ)句] [group by 分組字段] [order by 排序字段列表 desc] [LIMIT startrow,rownumber] 1、Select 字段列表 From 數據表 例:①、select id,gsmc,add,tel from haf (* 表示數據表中所有字段) ?、?、select 單價(jià),數量,單價(jià)*數量 as 合計金額 from haf (As 設置字段的別名) 2、Select … from … Where 篩選條件式 篩選條件式:①、字符串數據: select * from 成績(jì)單 Where 姓名='李明' ?、?、萬(wàn)用字符: select * from 成績(jì)單 Where 姓名 like '李%' select * from 成績(jì)單 Where 姓名 like '%李%' select * from 成績(jì)單 Where 姓名 like '%李_' ?、?、特殊的條件式: ?、? / > / < / <> / >= / <= ?、艫ND(邏輯與) OR(邏輯或) NOT(邏輯非) ?、荳here 字段名稱(chēng) in(值一,值二) ?、萕here 字段名稱(chēng) Is Null / Where 字段名稱(chēng) 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(倒,如果直接寫(xiě)為順序) 5、Select … from … LIMIT ".$start_rowno.",".($pagesize+1) 第二節 SQL語(yǔ)句實(shí)例應用 數據庫說(shuō)明: student(學(xué)生表): stdid int(11) id號 son char(5) 學(xué)號 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) 學(xué)分 sc(學(xué)生選課表): scid int(11) id號 cno char(5) 課程號 grade float 成績(jì) sno char(5) 學(xué)號 單表查詢(xún): 一、選擇表中的若干字段: 查詢(xún)指定列: 1、查詢(xún)全體學(xué)生的學(xué)號與姓名; select son,sname from student 2、查詢(xún)全體學(xué)生的姓名、學(xué)號、所在系; select sname,son,sdept from student 3、查詢(xún)全體學(xué)生的詳細記錄; select * from student 查詢(xún)經(jīng)過(guò)計算的值: 4、查全體學(xué)生的姓名及其出生年份 select sname,year(now())-sage as '出生年份' from student 5、查詢(xún)全體學(xué)生的姓名、出生年份和所有系,要求用大(小)寫(xiě)字母表示所有系名 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、查詢(xún)選修了課程的學(xué)生學(xué)號 select distinct sno from sc 查詢(xún)滿(mǎn)足條件的記錄: 比較大?。?/p> 7、查詢(xún)計算機全體學(xué)生的名單 select sname from student where sdept='cs' 8、查詢(xún)所有年齡在20歲以下的學(xué)生姓名及其年齡 select sname,sage from student where sage<20 9、查詢(xún)考試成績(jì)小于90分的學(xué)生的學(xué)號 select distinct sno from sc where grade<90 確定范圍: 10、查詢(xún)年齡在18-20歲之間的學(xué)生的姓名、系別和年齡。 select sname,sdept,sage from student where sage between 18 and 20 11、查詢(xún)年齡不在19-20歲之間的學(xué)生的姓名、系別和年齡。 select sname,sdept,sage from student where sage not between 19 and 20 確定集合: 12、查詢(xún)信息系(is)、數學(xué)系(ma)和計算機科學(xué)系(cs)學(xué)生的姓名和性別。 select sname,ssex from student where sdept in('is','ma','cs') 13、查詢(xún)不是信息系(is)、數學(xué)系(ma)的學(xué)生的姓名、系別和年齡。 select sname,ssex from student where sdept not in('is','ma') 字符匹配(like '<匹配串>' %代表任意長(cháng)度(長(cháng)度可以為0)的字符串 ; _代表任意單個(gè)字符,漢字得用兩個(gè)"__"): 14、查詢(xún)學(xué)號為95001的學(xué)生的詳細情況 select * from student where son like '95001' 15、查詢(xún)所有姓名李的學(xué)生的姓名、學(xué)號和性別。 select sname,son,ssex from student where sname like '李%' 16、查詢(xún)姓名是兩個(gè)字學(xué)生的姓名、學(xué)號和性別。 select sname,son,ssex from student where sname like '____' 17、查詢(xún)所有不姓李的學(xué)生姓名。 select sname from student where sname not like '李__' 涉及空值的查詢(xún): 18、某些學(xué)生選修課程后沒(méi)有參加考試,所以有選課記錄,但沒(méi)有考試成績(jì),查詢(xún)缺少成績(jì)的學(xué)生的學(xué)號和相應的課程號。 select sno,cno from sc where grade is null 19、查詢(xún)所有有成績(jì)的學(xué)生學(xué)號和課程號。 select sno,cno from sc where grade is not null 多重條件查詢(xún)(and or): 20、查詢(xún)計算機系年齡在20歲的學(xué)生姓名。 select sname from student where sdept='cs' and sage=20 21、查詢(xún)信息系(is)、數學(xué)系(ma)和計算機科學(xué)系(cs)學(xué)生的姓名和性別。 select sname,ssex from student where sdept='is' or sdept='ma' or sdept='cs' 三、對查詢(xún)結果排序: 22、查詢(xún)選修了3號課程的學(xué)生的學(xué)號及其成績(jì),查詢(xún)結果按分數的降序排列。 select sno,grade from sc where cno='3' order by grade desc 23、查詢(xún)全體學(xué)生情況,查詢(xún)結果按所在系的系號升序排列,同一系中的學(xué)生按年齡降序排列。 select * from student order by sdept,sage desc 四、使用集函數: 24、查詢(xún)學(xué)生總人數。 select count(*) as '總人數' from student 25、查詢(xún)選修了課程的學(xué)生人數。 select count(distinct sno) as '人數' from sc 26、計算1號課程的學(xué)生平均成績(jì) select format(avg(grade),2) as '平均成績(jì)' from sc where cno='1' 27、查詢(xún)選修1號課程的學(xué)生最高分數。 select max(grade) from sc where cno='1' 五、對查詢(xún)結果分組: 28、求各個(gè)課程號及相應的選課人數。 select cno as '課程號',count(sno) as '人數' from sc group by cno 29、查詢(xún)選修了3門(mén)以上課程的學(xué)生學(xué)號。 select sno from sc group by sno having count(*)>2 注:where 子句與 having 短語(yǔ)的區別在于作用對象不同,where 子句作用于基本表或視圖,從中選擇滿(mǎn)足條件的記錄,having短語(yǔ)作用于組,從中選擇滿(mǎn)足條件的組。 多表查詢(xún) 同時(shí)查詢(xún)兩個(gè)以上的表,稱(chēng)為連接查詢(xún)。 等值連接:當連接運算符為=時(shí),為等值連接。 1、查詢(xún)每個(gè)學(xué)生及其選修課程的情況(等值連接)。 select student.*,sc.* from student,sc where student.son=sc.sno 自然連接:在等值連接中把目標列中重復的屬性列去掉。 2、查詢(xún)每個(gè)學(xué)生及其選修課程的情況(自然連接)。 select student.son,sname,ssex,sage,sdept,cno,grade from student,sc where student.son=sc.sno 自身連接:連接操作不僅可以在兩個(gè)表之間進(jìn)行,也可以是一個(gè)表與其自己進(jìn)行連接。 3、查詢(xún)每一門(mén)課的間接先修課。 select a.cno,b.cpno,a.cname from course a,course b where a.cpno=b.cno 復合條件連接: 4、查詢(xún)選修2號課程且成績(jì)在90分以上的所有學(xué)生。 select a.son,sname from student a,sc b where a.son=b.sno and b.cno='2' and b.grade>90 5、查詢(xún)每個(gè)學(xué)生的學(xué)號、姓名、選修的課程名及成績(jì)。 select a.son,sname,cname,grade from student a,sc b ,course c where a.son=b.sno and b.cno=c.cno |
聯(lián)系客服