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