|
(一) 單表查詢
n 查詢表中全部記錄
select * from 表
n 查詢表中指定欄位記錄
select 欄位,欄位,...from 表
n 查詢前n條記錄
select top n percent * from 表
n 帶條件的查詢
select * from 表 where 條件
n 查詢去掉重複項
select distinct 欄位 from 表
n 查詢使用彙總函式(並起別名)
max() sum() min() avg() count()
select A=B from 表
select B a from 表
select B as a from 表
n 模糊查詢
like _單字元 %一個或多個字元 [] 範圍內 [^ ]不在範圍
n 查詢時對欄位排序
n 合并查詢結果
union
n 查詢建立新表
select 欄位,欄位 into 新表名 from 表
n 查詢使用分組命令.
select * from stuinfor
--統計女學生的平均年齡
select AVG(stuage )from stuinfor where ssex=‘女‘
--統計男學生的平均年齡
select AVG(stuage )from stuinfor where ssex=‘男‘
--分別統計男女學生的平均年齡
select stusex,AVG(stuage) from stuinfor group by stusex
(二) 多表查詢
n 交叉串連
select * from stuinfor,cinfor,scinfor
n 內串連(正確)
select 欄位,欄位 from 表,表 where 表.欄位=表.欄位
select 欄位,欄位 from 表 inner join 表 on 表.欄位=表.欄位 and ...
n 外串連outer join
l 左外串連
(LEFT join) 左表全顯右表滿足顯 不滿足以null顯示
l 右外串連
(right join)右表全顯左表滿足顯 不滿足以null顯示
l 完全串連
(full join)左+右
//(1)查詢學生資訊表中的全部記錄
select * from stuinfor
//(2)查詢學生資訊表中學生的學號,姓名,學院
Select sno,sname,scollege from stuinfor
//(3)查詢學生資訊表中軟工或動畫學院學生資訊
Select * from stuinfor where scollege=‘軟工‘ or scollege=‘動畫‘
select * from stuinfor where scollege in(‘軟工‘,‘動畫‘)
//(4)查詢學生資訊表中年齡在23—27之間學生資訊
Select * from stuinfor where sage between 23 and 27
select * from stuinfor where sage>=23 and sage<=27
//(5)查詢學生資訊表中年齡>28並且是軟工的學生資訊
Select * from stuinfor where sage<28 and scollege=‘軟工‘
//(6)查詢學生資訊表中前五條記錄的學號,姓名,性別
Select top 5 sno,sname,ssex from stuinfor
//(7)查詢學生資訊表中學生來自哪些學院(幾個)
Select scollege from stuinfor
(select count(distinct scollege )from stuinfor)
//(8)查詢學生資訊表中學生的最大年齡及最小年齡
Select max(sage) 最大值 ,min(sage) 最小值 from stuinfor
//(9)查詢成績表中選修1號課程的總分及平均分
Select sum(score) ,avg(score) from scinfor where cno=1
//(10)查詢成績表中成績合格的個數
Select count(score) from scinfor where score>=60
//(11)按由高到低的順序查詢學產生績表中的學生資訊
Select * from scinfor order by score desc
//(12)查詢姓李的學生資訊
Select * from stuinfor where sname like ‘%李‘
//(13)分別統計每個學院的平均年齡
Select avg(sage) from stuinfor group by scollege
//(14)查詢學生的學號,姓名,學院建立一個新表n1
Select sno,sname,scollege into n1 from stuinfor
//(15)查詢所有選修了Java課程的學生的學號,姓名,性別,學院及成績
select stuinfor.sno,sname,ssex,scollege,scroe from stuinfor,cinfor,scinfor
where scinfor.sno=stuinfor.sno and scinfor.cno=cinfor.cno and cname=‘java‘
select stuinfor.sno,sname,ssex,scollege,scroe
from stuinfor inner join scinfor on stuinfor.sno =scinfor.sno
inner join cinfor on scinfor.cno=cinfor.cno
and cname=‘java‘
//(16)查詢所有學生的選課情況
select * from stuinfor left join scinfor on stuinfor.sno=scinfor.sno
|