- 基本語(yǔ)法
Select [all | distinct] select_fields_list -- all 顯示所有含重復(fù)數(shù)據(jù) distinct 不顯示重復(fù)數(shù)據(jù)
From table_list/view_list -- 表或視圖 可以是一個(gè)或多個(gè)表
[Where conditions] -- 條件,可以在一個(gè)表 或 多個(gè)表
[group by group_list] -- 按需要分組
[having conditions] -- 用于分組的條件 相當(dāng)于group 的 where
[order by order_list] -- 按制定要求排序 asc升 desc降 newid()隨機(jī)排序,其他有用random()的
查詢語(yǔ)句執(zhí)行順序堕扶,一般為
FROM JOIN -- WHERE -- GROUP BY -- HAVING -- SELECT -- ORDER BY -- LIMIIT
- 比較運(yùn)算符
=徊件、>、< 匆背、<>呼伸、(!=)、>=钝尸、<=括享、!>、!<
not蝶怔、and 奶浦、or、between..and -- Where 工資 between 1000 and 2000
in(列表值1踢星、列表值2澳叉、……..)
is (not) null空值查詢
like 模糊查詢
% 包含0個(gè)或多個(gè)字符
_ 包含一個(gè)字符
[ ] 指定范圍如[a-z]、[1-9]
[^] 不屬于指定范圍 [^a-z]
- 查詢例子
- 例子數(shù)據(jù)表
use db_mysql
go
create table 雇員
(
雇員編號(hào) int primary key,
姓名 varchar(20) not null,
性別 char(2) check(性別='男' or 性別='女')default'男',
工資 int
)
use db_mysql
go
insert into 雇員(雇員編號(hào),姓名,性別,工資) values(101,'B','男',1200)
insert into 雇員(雇員編號(hào),姓名,性別,工資) values(102,'C','男',1500)
insert into 雇員(雇員編號(hào),姓名,性別,工資) values(103,'D','女',1400)
- 類型轉(zhuǎn)換
use db_mysql
go
select *,CAST(工資*0.08 as decimal(18,1))as 獎(jiǎng)金 from 雇員
--Cast(字段名 as 轉(zhuǎn)換的類型):類型轉(zhuǎn)換
-- 其中類型可以是 bianary char date datetime decimal signed int time unsigned
- 輸出一個(gè)連接的字符串信息
use db_mysql
go
select ('雇員編號(hào)是'+cast(雇員編號(hào) as varchar(20))+','+'工資是'+cast(工資 as varchar(20)))as 信息 from 雇員 where 工資=1000
--顯示字符串時(shí)沐悦,其他類型要通過(guò)cast轉(zhuǎn)換為字符型
- 條件輸出信息 between..and
use db_mysql
go
select 姓名,工資狀況 =
case
when 工資>900 and 工資<=1200 then '低'
when 工資>1200 and 工資<=1400 then '高'
end
from 雇員
- 等值查詢 in
use db_mysql
go
select * from 訂單 where 員工號(hào) in (select 雇員編號(hào) from 雇員 where 姓名='A' )
select * from 訂單 where 員工號(hào) in (100成洗,101,102)
select * from 公司 where 公司號(hào) not in (select 公司號(hào) from 雇員) --沒(méi)有雇員的公司
- 模糊查詢 Like
use db_mysql
go
select * from 雇員 where 工資 like '_[2-4]00' #1200 1300 1400
like 模糊查詢
% 包含0個(gè)或多個(gè)字符
_ 包含一個(gè)字符
[ ] 指定范圍如[a-z]藏否、[1-9]
[^] 不屬于指定范圍 [^a-z]
- 高級(jí)查詢
- 聚合函數(shù)
對(duì)一組數(shù)據(jù)進(jìn)行計(jì)算并返回單一值瓶殃,也稱為多行函數(shù)或組合函數(shù)。對(duì)整個(gè)數(shù)據(jù)集合進(jìn)行計(jì)算副签,返回一行匯總結(jié)果
函數(shù)名 | 功能 |
---|---|
count(*) | 統(tǒng)計(jì)選擇的記錄個(gè)數(shù) |
count() | 統(tǒng)計(jì)特定列中值的個(gè)數(shù) |
sum() | 計(jì)算總和(必須是數(shù)值) |
avg() | 平均值 |
max() | 最大值 |
min() | 最小值 |
* count()遥椿,avg,sum可以使用 distinct關(guān)鍵字淆储,計(jì)算時(shí)不包含重復(fù)行冠场。count(*)、min本砰、max由于不改變結(jié)果碴裙,沒(méi)必要用
- 查詢例子
select count(distinct(公司號(hào)))as 無(wú)重復(fù)公司個(gè)數(shù) from 雇員
select count(*) as 2號(hào)公司人數(shù),sum(工資)as 工資總和 from 雇員
where 公司號(hào) in (select 公司號(hào) from 公司 where 城市='上海')
select * from 雇員 where 工資=(select max(工資)from 雇員)
- 分組查詢
group by : 分組前的條件 用 where 分組后的條件用having
分組執(zhí)行聚合函數(shù)
例如:按公司號(hào)分組,顯示每個(gè)公司 男 員工的平均工資 大于1700的公司
select 公司號(hào), avg(工資) as 不同公司平均工資 from 雇員
where 性別='男' group by 公司號(hào)
having avg(工資)>1700 -- 顯示大于1700的
- 多列分組查詢
select 公司號(hào),性別点额,AVG(工資) as 不同公司的平均工資 from 雇員
where 性別 is not null and 工資>2100 group by 公司號(hào), 性別
select 公司號(hào),性別舔株,AVG(工資) as 不同公司的平均工資 from 雇員
where 性別 is not null and 工資>2100 group by 公司號(hào), 性別
having AVG(工資) > 1600
all: 在group 分組中即使沒(méi)有符合查詢條件的內(nèi)容,分組字段也會(huì)顯示还棱。
select 公司號(hào), max(工資) as 最大工資 from 雇員 where 工資>1900
group by all 公司號(hào)
--- 即使有公司 工資都小于1900载慈,公司號(hào)也會(huì)顯示,最大工資會(huì)顯示null
-
SQL嵌套查詢
用另一個(gè)查詢語(yǔ)句的結(jié)果進(jìn)行查詢珍手。
select * from 學(xué)生 where 分?jǐn)?shù) = (select max(分?jǐn)?shù))from 學(xué)生)
select * from 學(xué)生 where 分?jǐn)?shù)>( select avg(分?jǐn)?shù)) from 學(xué)生) and 分?jǐn)?shù)<(select max(分?jǐn)?shù)) from 學(xué)生)
--也可以用between ( select avg(分?jǐn)?shù)) from 學(xué)生) and (select max(分?jǐn)?shù)) from 學(xué)生)
-- 可以多級(jí)嵌套
- in
select * from 學(xué)生 where 分?jǐn)?shù) in (select max(分?jǐn)?shù))from 學(xué)生)娃肿,(select min(分?jǐn)?shù))from 學(xué)生))
- exists 嵌套返回True 或 False
select * from 班級(jí) where exists(select * from 學(xué)生 where 班級(jí)號(hào) = 學(xué)生.班級(jí)號(hào) and 分?jǐn)?shù)=100)
-- 顯示班上有滿分學(xué)生 的 班級(jí)信息
-
多表連接查詢JOIN
連接查詢是橫向結(jié)合咕缎,將兩個(gè)關(guān)系庫(kù)表接成一個(gè)更寬的關(guān)系表,包括四種:內(nèi)聯(lián)查詢料扰、左連接查詢凭豪、右連接查詢、全連接查詢晒杈。多表集合的并嫂伞、交 和 差運(yùn)算,也是多表查詢拯钻。
- 多表查詢
如果不設(shè)置連接條件帖努,則會(huì)形成笛卡爾積,即多個(gè)表的組合粪般,比如一個(gè)表有3行另一個(gè)表有10行拼余,連接后一個(gè)表有3x10 = 30 行。
select 姓名,分?jǐn)?shù),地址 from 學(xué)生亩歹,家庭 -- 組合
-- 學(xué)生[姓名 分?jǐn)?shù) 年齡 家庭編號(hào)] 家庭[編號(hào)匙监,父親 目前 地址]
select 姓名,分?jǐn)?shù),地址 from 學(xué)生,家庭 where 學(xué)生.家庭編號(hào) = 家庭.編號(hào)
select 姓名小作,工資亭姥,城市 from 雇員,公司 where 雇員.公司號(hào) = 公司.公司號(hào) and
(工資 between 600 and 999)and 城市 = '上海'
- 多表連接查詢生成新表 into
有些操作是無(wú)法利用多表查詢的顾稀,比如group by 必須將多表查詢結(jié)果生成一個(gè)新表达罗,對(duì)新表進(jìn)行分組。
select xxx into 連接的新表 from 雇員, 公司 where xxx
select xxx from 連接的新表 group by 公司
- 超連接查詢静秆,即內(nèi)聯(lián)查詢粮揉、左連接查詢、右連接查詢抚笔、全連接查詢滔蝉,使用超連接查詢不僅可以將滿足條件的記錄顯示出來(lái),還可以吧一部分不滿足條件的記錄以NULL顯示塔沃。
(1)內(nèi)連接查詢(inner join)使用比較運(yùn)算對(duì)表之間的某些數(shù)據(jù)進(jìn)行比較,并列出這些表中與連接條件相匹配的數(shù)據(jù)行阳谍。
--顯示雇員姓名及公司坐在地
select 姓名蛀柴,城市 from 公司 inner join 雇員 on 雇員.公司號(hào) = 公司.編號(hào)
and 城市 is not null and 姓名 like'%美%'
--多表內(nèi)聯(lián)查詢
select xxx from 公司
inner join 雇員 on 雇員.公司號(hào) = 公司.編號(hào)
inner join 訂單 on 雇員.雇員號(hào) = 訂單.雇員號(hào)
and 工資 > 1000 and 訂單金額>9999
(2) 左連接查詢:除了顯示滿足連接條件的記錄之外,第一張表中 不滿足條件的記錄 也顯示在結(jié)果中矫夯。
select xxx from 公司
left join 雇員 on 雇員.公司號(hào) = 公司.編號(hào)
left join 訂單 on 雇員.雇員號(hào) = 訂單.雇員號(hào)
and 工資 > 1000 and 訂單金額>9999
-- 第一個(gè)left jion 第一張表是 公司鸽疾,第二張表是雇員;第二個(gè)left join 第一張表是雇員 第二張表是 訂單
(3)右連接查詢:除了顯示滿足連接條件的記錄之外训貌,第二張表中 不滿足條件的記錄 也顯示在結(jié)果中制肮。
(4)全連接查詢:除了顯示滿足連接條件的記錄之外冒窍,兩張表中 不滿足條件的記錄 也顯示在結(jié)果中。
·
select xxx from 公司
left join 雇員 on 雇員.公司號(hào) = 公司.編號(hào)
right jion 訂單 on 雇員.雇員號(hào) = 訂單.雇員號(hào)
and 工資>600
- 查詢集合的并豺鼻、交综液、差
SQL支持集合的并運(yùn)算(union)、交運(yùn)算(intersect)和差運(yùn)算(except)儒飒,也就是將幾個(gè)select的結(jié)果集進(jìn)行運(yùn)算合并成一個(gè)查詢結(jié)果谬莹。為了進(jìn)行運(yùn)算,兩個(gè)查詢結(jié)果具有相同的字段個(gè)數(shù)桩了,并且對(duì)應(yīng)的字段要具有相同的數(shù)據(jù)類型 和 取值范圍附帽。
-- 顯示城市在 上海 和 工資>999的公司信息
select 編號(hào) from 公司 where 城市 = ‘上海’
union
select 公司號(hào) from 雇員 where 工資>999