今天主要的內容是要講解SQL中關於Join、Inner Join、Left Join、Right Join、Full Join、On、 Where區別和用法,不用我說其實前面的這些基本SQL語法各位攻城獅基本上都用過。可是每每咱們可能用的比較多的也就是左右鏈接和內鏈接了,並且對於許多初學者而言不知道何時該用哪一種語法進行查詢,而且對於左右,或者內鏈接查詢的時候關於ON 和Where 的做用也是模糊不清的,說不出其中的一個大概的差異,所以接下來請容我把它們好好描述一遍。數據庫
概念:用於兩表或多表之間數據聯立查詢3d
select * from Students s,Class c where s.ClassId=c.ClassId
概念:與Join相同,兩表或多表之間聯立查詢數據,所以咱們在使用多表join查詢的時候既可使用where關聯,也能夠是inner join關聯查詢blog
select * from Students s inner join Class c on s.ClassId=c.ClassId
概念:以左表中的數據爲主,即便與右表中的數據不匹配也會把左表中的全部數據返回class
select * from Students s left join Class c on s.ClassId=c.ClassId
概念:與Left Join的用法相反,是以右表中的數據爲主,即便左表中不存在匹配數據也會把右表中全部數據返回基礎
select * from Students s right join Class c on s.ClassId=c.ClassId
概念:返回表中全部的數據數據,不管匹配與否select
select * from Students s Full JOIN Class c on s.ClassId=c.ClassId
select * from Students s inner JOIN Class c on s.ClassId=c.ClassId and s.Sex='男'
select * from Students s left join Class c on s.ClassId=c.ClassId and s.Sex='男'
--全鏈接 select * from Students s full join Class c on s.ClassId=c.ClassId --全鏈接加on查詢 select * from Students s full join Class c on s.ClassId=c.ClassId and s.Sex='男'
一、on條件是在生成臨時表時使用的條件,它無論on中的條件是否爲真,都會返回左邊表中的記錄(以左鏈接爲例)。
二、where條件是在臨時表生成好後,再對臨時表產生的數據進行過濾條件篩選。語法
最後我想說的是,有時候咱們總認爲概念性的東西很簡單而忽視了實踐。其實每每一些基礎性的東西纔是咱們爲日後構造的萬丈高樓的前提,堅持實踐,堅持動手,你會發現許多你沒有想到過的問題喲!im