SQL中的表连接及子查询
一、表連接
SQL Server支持多種連接包括:內(nèi)連接,左連接,右連接,交叉連接,全外連接。
每種連接類(lèi)型指定SQL Server如何使用一個(gè)表中的數(shù)據(jù)來(lái)選擇另一個(gè)表中的行
1.內(nèi)連接
內(nèi)連接是查詢出兩個(gè)表相關(guān)聯(lián)的部分。
select select_list from T1 inner join T2 on join_predicate內(nèi)連接關(guān)鍵字:inner join ......on......,on后面跟查詢條件。from后面跟的表為主表,inner join后面跟的表為副表。(后面的左連接,右連接,交叉連接,自連接也同樣適用)
2.左連接
left join子句用于查詢多個(gè)表的數(shù)據(jù)。它返回左表中的所有行和右表中的匹配行,如果右表中找不到匹配行,則返回NULL值
selectselect_list fromT1 left join T2 onjoin_predicate關(guān)鍵字:left join.....on......
3.右連接
selectselect_list fromT1 left join T2 onjoin_predicate關(guān)鍵字:right join.....on......
4.交叉連接
cross join連接兩個(gè)或多個(gè)不相關(guān)的表
以下是兩個(gè)表的SQL Server join的語(yǔ)法cross join將第一個(gè)表中的每一行與第二個(gè)表中的每一行連接起來(lái)。換句話說(shuō),交叉連接返回兩個(gè)表中行的笛卡爾積。與inner join或left join不同,交叉連接不會(huì)再連接的表之間建立關(guān)系
SELECTselect_list FROMT1 CROSS JOIN T25.自連接
自連接不在五大連接之內(nèi)。自連接用于將表連接到自身(同一個(gè)表)。它對(duì)于查詢分層數(shù)據(jù)或比較同一個(gè)表中的行很有用。
自連接使用內(nèi)連接或左連接子句。由于使用自連接的查詢引用同一個(gè)表。因此表別名用于為查詢中的表分配不同的名稱。
如果在不使用表別名的情況下多次引用同一個(gè)表,則會(huì)出現(xiàn)錯(cuò)誤
SELECTselect_list FROM T t1 [inner|left] join T t2 onjoin_predicate;6.全外連接
關(guān)鍵字:full outer join
當(dāng)左表或右表中存在匹配項(xiàng)時(shí),該命令將返回所有的行
select * from pm.projects p full outer join pm.members m on p.id=m.project_id原本兩個(gè)表有關(guān)聯(lián),但是名稱不一樣
進(jìn)行全外鏈接后得到
約等于左連接和右連接進(jìn)行合并,關(guān)聯(lián)不上的都給null值
二、子查詢
1.子查詢
select * from sales.orders where customer_id in( select customer_id from sales.customers where city='New York' )子查詢時(shí)嵌套在另一個(gè)語(yǔ)句中如:(select,insert,update或delete)中的查詢
??
2.嵌套子查詢
子查詢可以嵌套在另一個(gè)子查詢中,SQL Server最多支持32個(gè)嵌套級(jí)別
--查找價(jià)格高于'上海永久','鳳凰'品牌的所有產(chǎn)品的平均定價(jià)的產(chǎn)品 select * from production.products where list_price >( select avg(list_price) from production.products where brand_id in (selectbrand_idfromproduction.brandswherebrand_name in('上海永久','鳳凰') ) )?最終的出的結(jié)果如下圖所示,共161條數(shù)據(jù)
?
3.相關(guān)子查詢
相關(guān)子查詢是外部查詢的值的【子查詢】。換句話說(shuō),它取決于外部有查詢的值,由于這種依賴性,相關(guān)子查詢不能作為簡(jiǎn)單的子查詢獨(dú)立執(zhí)行
此外,對(duì)外部查詢?cè)u(píng)估的每一行重復(fù)執(zhí)行一次相關(guān)子查詢
相關(guān)子查詢也稱為重復(fù)子查詢
---示例查找價(jià)格等于其類(lèi)別的最高價(jià)格的產(chǎn)品 ---子查詢 select * from production.products p1 inner join(select category_id,max(list_price)max_pricefrom production.productsgroup by category_id)p2on p1.category_id=p2.category_idand p1.list_price=p2.max_price---相關(guān)子查詢 select p1.* from production.products p1 where p1.list_price in(select max(list_price)max_pricefrom production.products p2where p1.category_id=p2.category_idgroup by category_id ) select p1.* from production.products p1 order by category_id4.Exists運(yùn)算符
Exists運(yùn)算符是一個(gè)邏輯運(yùn)算符,用于檢查子查詢是否放回任何行,如果子查詢返回一行或多行,則Exists運(yùn)算符返回true以下是SQL Server Exists運(yùn)算符的語(yǔ)法:
Exists(subquery)在此語(yǔ)法中,子查詢僅是Select語(yǔ)句。子查詢返回行后,Exists運(yùn)算符返回true并立即停止處理
請(qǐng)注意,即使子查詢返回Null值,Exists運(yùn)算符也會(huì)計(jì)算為true
select * fromproduction.products p1 where exists(select * fromproduction.products p2wherep1.category_id = p2.category_idand p1.list_price>8000)?
5.any運(yùn)算符
--查找其他品牌價(jià)格大于‘優(yōu)米優(yōu)品牌的任意產(chǎn)品的價(jià)格的產(chǎn)品’ select * fromproduction.products wherelist_price>any (select list_price from production.productswherebrand_id =9) andbrand_id!=9 order by list_price某個(gè)值大于any()指大于any中任意一個(gè)值,有可能大于最小值,有可能大于最大值
6.all運(yùn)算符
--查找其他品牌價(jià)格大于‘優(yōu)米優(yōu)’品牌任何產(chǎn)品的價(jià)格的產(chǎn)品 select * fromproduction.products wherelist_price>all (selectlist_price from production.products where brand_id=9) andbrand_id!=9 order by list_price?
當(dāng)要求的數(shù)據(jù)大于all時(shí)是大于最大值,小于all時(shí)是最小值
總結(jié)
以上是生活随笔為你收集整理的SQL中的表连接及子查询的全部?jī)?nèi)容,希望文章能夠幫你解決所遇到的問(wèn)題。
- 上一篇: 【C++要笑着学】类的默认成员函数详解
- 下一篇: mysql数据库首次查询缓慢