SQL中的有關的查詢語句

數據庫中有兩張表,內容如下:

--*查詢全部字段 
select * from stuInfo
--查詢部分字段
select stuId,stuAge,StuSex from stuInfo
--設置字段別名
select stuId 學號,stuAge as '年齡','性別'=StuSex from stuInfo

執行效果

字段連接

---varchar是連接符 
select stuName+'==>'+stuSex as '姓名-性別' from stuInfo

執行效果

當-+ 當連接的字符是int型時就變成了了加法計算

---+ int就是加法計算
select stuId +'-'+stuAge as '學號-年齡' from stuInfo
select * from stuInfo

--關鍵詞 all 代表所有數據  也可以省略
select * from stuInfo
select all * from stuInfo
select all stuId from stuMarks 
--distinct過濾重複記錄
select stuId from stuMarks 
select distinct stuId from stuMarks
--top 前 n 條 內容
select * from stuMarks
select  Score from stuMarks 
--top 3 顯示錶裏面的前3條數據
select top 3  Score from stuMarks 
--top 50 percent 顯示錶裏面的前50%的數據
select top 50 percent Score from stuMarks
--條件查詢
select * from stuInfo where stuSex='女'
select * from stuInfo where stuSex='男' and stuAge>30
select * from stuInfo where stuAge <>32
select * from stuInfo where stuAge !=32
--between and範圍查詢
select * from stuInfo where stuAge>=20 and stuAge<=30
select * from stuInfo where stuAge between 20 and 30
select * from stuInfo where stuAge not between 20 and 30
--in  散列值   顯示在()裏面的數據
select * from stuInfo where stuAge in (20,32)
select * from stuInfo where stuAge not in (20,32)
--null 判斷使用is null
select * from stuMarks where Score is null
select * from stuMarks where Score is not null
--模糊查詢line搭配 %佔0個或多個位置  _佔一個位置 [] [^]
select * from stuInfo where stuName like '%三'
select * from stuInfo where stuName like '三%'
select * from stuInfo where stuName like '%三%'
--_佔一個位置
select * from stuInfo where stuName like '_三'
select * from stuInfo where stuName like '%三_'
--[]佔一個位置且內容是中括號裏面的內容 [^]佔一個位置且內容不是中括號裏面的內容
select * from stuInfo where stuName like '%[三,四,五]'
select * from stuInfo where stuName like '%[三四五]'
select * from stuInfo where stuName like '%[^三四五]'
--order by排序  默認是升序asc  降序desc
select * from  stuMarks where CouName='java' order by Score 
select top 1 * from  stuMarks where CouName='java' order by Score 
select * from stuMarks where couName='html' order by Score desc
--聚合函數   查詢字段必須包含在聚合函數中
--avg()  平均值
select Avg(Score) from stuMarks where CouName='html' 
select sum(Score) from stuMarks where CouName='java'
select max(Score) as 'java最高分' from stuMarks where  CouName='java'
select min(Score) as 'java最低分' from stuMarks where  CouName='java'
select count(couId) as '參加java考試總人數' from stuMarks where CouName='java'
-- group by 分組 字段必須包含在group by字句中   一般搭配聚合函數一塊使用
select stuSex from stuInfo group by stuSex
--展示考了哪幾門試題,計算每門考試的平均分
select couName,avg(Score) as '平均分' from stuMarks group by CouName
--展示平均分大於80分的科目  having通常和group by一起使用(篩選條件)
select CouName ,avg(Score) '平均分' from stuMarks group by CouName having avg(Score)>80

表的聯合查詢

--表的聯合查詢  inner join
select stuInfo.stuId '學號',stuName '姓名',couName '科目',Score '分數' from stuInfo inner join stuMarks on stuInfo.stuId=stuMarks.stuId

--並集   (all就是把重複結果顯示兩次)
select * from stuInfo where stuAge>30
union all
select * from stuInfo where stuSex='男'
select * from stuInfo where stuAge>30 or stuSex='男'
--交集 兩個查詢結果的交集
select * from stuInfo where stuAge>30
intersect
select * from stuInfo where stuSex='男'
select * from stuInfo where stuAge>30 and stuSex='男'
--差集
select * from stuInfo where stuAge>30
except
select * from stuInfo where stuSex='男'
select * from stuInfo where stuAge>30 and stuId not in (select stuId from stuInfo where stuAge>30 and stuSex='男')

 

 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章