數(shù)據(jù)的基本查詢

理解查詢

服務(wù)器執(zhí)行命令,在原始數(shù)據(jù)表中查找符合條件的數(shù)據(jù),產(chǎn)生一個(gè)虛擬表。
虛擬表是數(shù)據(jù)組合后的重新展示浴鸿,而不是原始的物理數(shù)據(jù)。

查詢基本語法構(gòu)成

select <列名>
from <表名>
[where <查詢條件表達(dá)式>]
[order by <排序的列名> ASC或DESC]

查詢?nèi)啃泻土?/h4>
select * from Students

查詢部分行

select StudentName,Gender,出生日期
from Students where Gender='男' and Age>20

使用“AS”或使用“=”重新命名字段

select StudentName as 姓名,Gender as 性別,出生日期=birthday
from Students where Gender='男'

加號的使用

select 姓名=StudentName,地址和電話=StudentAddress+'【'+PhoneNumber+'】' 
from Students where Gender='男'
select 總成績=CSharp+SQLServerDB from ScoreList

注意:
1.+連接的數(shù)據(jù)類型必須兼容
2.如果使用+連接字符型數(shù)據(jù)弦追,結(jié)果為字符串?dāng)?shù)據(jù)的連接
3.如果使用+連接數(shù)值型數(shù)據(jù)岳链,結(jié)果為數(shù)值的和

查詢空列

select * from ScoreList where SQLServerDB is null

使用常量列

select StudentName as 姓名,Gender as 性別,出生日期=birthday,所在學(xué)校='北京大學(xué)'
from Students where Gender='男'

限制固定行數(shù)

select top 5 StudentName,Gender,Birthday from Students

返回百分之多少行

select top 20 percent StudentName,Gender,Birthday from Students

按多列排序

select top 3 StudentId,CSharp as C#,DB=SQLServerDB
from ScoreList  
where StudentId not in(select top 6 StudentId from ScoreList order by SQLServerDB DESC,CSharp DESC )
order by SQLServerDB DESC,CSharp DESC

模糊查詢-like

select StudentName,StudentAddress from Students where StudentAddress like '天津%'
select StudentName,StudentAddress from Students where StudentName like '%小%'

模糊查詢-between

select * from ScoreList where CSharp between 80 and 90
select StudentName,StudentAddress,Birthday from Students where Birthday between '1987-01-01' and '1988-01-01'

模糊查詢-in

select StudentName,StudentAddress,age from Students where Age in(21,22,23)
select StudentName,StudentAddress,age from Students where StudentName in('王小虎','賀小張')

查詢函數(shù)的使用

聚合函數(shù)(求和、統(tǒng)計(jì)骗卜、求最大值、最小值左胞、平均值)

select SUM(CSharp) as C#總成績 from ScoreList
select 總?cè)藬?shù)=COUNT(*) from Students
select MAX(Csharp) as C#最高分 ,MIN(CSharp) as C#最低分,AVG(CSharp) as C#平均分 from ScoreList

多表之間的數(shù)據(jù)查詢

內(nèi)連接(inner join...on...)查詢
select Students.StudentId,C#成績=CSharp,StudentName,ClassName
from ScoreList
inner join Students on Students.StudentId=ScoreList.StudentId
inner join StudentClass on Students.ClassId=StudentClass.ClassId
where CSharp >80
左外連接(left outer join...on...)
select Students.StudentId,StudentName,Gender ,C#成績=CSharp from Students
left outer join ScoreList on Students.StudentId=ScoreList.StudentId
where Gender='男'
右外連接(right outer join...on...)

分組查詢寇仓、統(tǒng)計(jì)及統(tǒng)計(jì)篩選(group by...having...)

select 班級=StudentClass.ClassName,人數(shù)=COUNT(*),C#最高分=Max(CSharp),DB最高分=MAX(SQLServerDB),
AVG(CSharp) as C#平均分,AVG(SQLServerDB) as DB平均分
from Students
inner Join StudentClass on Students.ClassId =StudentClass.ClassId
inner join ScoreList on ScoreList.StudentId=Students.StudentId
group by ClassName
having AVG(CSharp)>=70 and AVG(SQLServerDB)>=70

查詢重復(fù)

--查詢所有重復(fù)的記錄
select * from ScoreList
where StudentId in(select StudentId from ScoreList group by StudentId  having COUNT(*)>1)
order by StudentId
--其它方法
select * from ScoreList
where (select COUNT(*) from ScoreList s where s.StudentId=ScoreList.StudentId)>1
order by StudentId

分組查詢對比

  • where字句:從數(shù)據(jù)源中去掉不符合其搜索條件的數(shù)據(jù)。
  • group by字句:搜集數(shù)據(jù)行到各個(gè)組中烤宙,統(tǒng)計(jì)函數(shù)為各個(gè)組計(jì)算統(tǒng)計(jì)值遍烦。
  • having字句:在分組結(jié)果中,去掉不符合其組搜索條件的各組數(shù)據(jù)行躺枕。
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末服猪,一起剝皮案震驚了整個(gè)濱河市,隨后出現(xiàn)的幾起案子拐云,更是在濱河造成了極大的恐慌罢猪,老刑警劉巖,帶你破解...
    沈念sama閱讀 222,807評論 6 518
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件叉瘩,死亡現(xiàn)場離奇詭異膳帕,居然都是意外死亡,警方通過查閱死者的電腦和手機(jī)薇缅,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 95,284評論 3 399
  • 文/潘曉璐 我一進(jìn)店門危彩,熙熙樓的掌柜王于貴愁眉苦臉地迎上來攒磨,“玉大人,你說我怎么就攤上這事汤徽∶溏郑” “怎么了?”我有些...
    開封第一講書人閱讀 169,589評論 0 363
  • 文/不壞的土叔 我叫張陵谒府,是天一觀的道長拼坎。 經(jīng)常有香客問我,道長狱掂,這世上最難降的妖魔是什么演痒? 我笑而不...
    開封第一講書人閱讀 60,188評論 1 300
  • 正文 為了忘掉前任,我火速辦了婚禮趋惨,結(jié)果婚禮上鸟顺,老公的妹妹穿的比我還像新娘。我一直安慰自己器虾,他們只是感情好讯嫂,可當(dāng)我...
    茶點(diǎn)故事閱讀 69,185評論 6 398
  • 文/花漫 我一把揭開白布。 她就那樣靜靜地躺著兆沙,像睡著了一般欧芽。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上葛圃,一...
    開封第一講書人閱讀 52,785評論 1 314
  • 那天千扔,我揣著相機(jī)與錄音,去河邊找鬼库正。 笑死曲楚,一個(gè)胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的褥符。 我是一名探鬼主播龙誊,決...
    沈念sama閱讀 41,220評論 3 423
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼喷楣!你這毒婦竟也來了趟大?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 40,167評論 0 277
  • 序言:老撾萬榮一對情侶失蹤铣焊,失蹤者是張志新(化名)和其女友劉穎逊朽,沒想到半個(gè)月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體曲伊,經(jīng)...
    沈念sama閱讀 46,698評論 1 320
  • 正文 獨(dú)居荒郊野嶺守林人離奇死亡惋耙,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點(diǎn)故事閱讀 38,767評論 3 343
  • 正文 我和宋清朗相戀三年,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片绽榛。...
    茶點(diǎn)故事閱讀 40,912評論 1 353
  • 序言:一個(gè)原本活蹦亂跳的男人離奇死亡湿酸,死狀恐怖,靈堂內(nèi)的尸體忽然破棺而出灭美,到底是詐尸還是另有隱情推溃,我是刑警寧澤,帶...
    沈念sama閱讀 36,572評論 5 351
  • 正文 年R本政府宣布届腐,位于F島的核電站铁坎,受9級特大地震影響,放射性物質(zhì)發(fā)生泄漏犁苏。R本人自食惡果不足惜硬萍,卻給世界環(huán)境...
    茶點(diǎn)故事閱讀 42,254評論 3 336
  • 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望围详。 院中可真熱鬧朴乖,春花似錦、人聲如沸助赞。這莊子的主人今日做“春日...
    開封第一講書人閱讀 32,746評論 0 25
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽雹食。三九已至畜普,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間群叶,已是汗流浹背吃挑。 一陣腳步聲響...
    開封第一講書人閱讀 33,859評論 1 274
  • 我被黑心中介騙來泰國打工, 沒想到剛下飛機(jī)就差點(diǎn)兒被人妖公主榨干…… 1. 我叫王不留街立,地道東北人舶衬。 一個(gè)月前我還...
    沈念sama閱讀 49,359評論 3 379
  • 正文 我出身青樓,卻偏偏與公主長得像几晤,于是被迫代替她去往敵國和親约炎。 傳聞我的和親對象是個(gè)殘疾皇子植阴,可洞房花燭夜當(dāng)晚...
    茶點(diǎn)故事閱讀 45,922評論 2 361