共計 1317 個字符,預計需要花費 4 分鐘才能閱讀完成。
這篇文章主要介紹“SQL 表連接的方法”,在日常操作中,相信很多人在 SQL 表連接的方法問題上存在疑惑,丸趣 TV 小編查閱了各式資料,整理出簡單好用的操作方法,希望對大家解答”SQL 表連接的方法”的疑惑有所幫助!接下來,請跟著丸趣 TV 小編一起來學習吧!
可以通過圖看下
多表查詢分為 內、外連接
外連接分為左連接(left join 或 left outer join)、右連接(right join 或者 right outer join)、和完整外部連接(full join 或者 full outer join)
左連接(left join 或 left outer join)的結果就是 left join 子句中的左表的所有行,而不僅僅是鏈接列所匹配的行,如果左表中的某行在右表中沒有匹配,則在相關聯的結果行中右表的所有選擇列均為空值(NULL)
SQL 語法 select * from table1 left join table2 on table1. 條件列名 = table2. 條件列名;
注釋:顯示的就是 table1 中的所有列和能匹配的列
右連接(right join 或 right outer join)在這里不做多說這左連接很象但是是相反的,只說一下語法
select *from table1 right join table2 on table1. 條件列 = table2. 條件列
完全外部連接(full join 或 full outer join)
顯示左右表中的所有行,當某一個表中沒有匹配的行時,則另一個表的選擇列表列包含空值(NULL)如果有則顯示全部數據
SQL 語法:
select *from table1 full join table2 on table1. 條件列名 = table2. 條件列名
內連接:
概念:內連接就是用比較運算符比較要用連接列的值的連接
內連接(join 或者 inner join)
SQL 語法:
select *fron table1 join table2 on table1. 條件列名 = table2. 條件列名
返回符合匹配條件的兩表列
等價于:
select A* ,B* from table1 A ,table2 B where A. 條件列名 =B. 條件列名
select *form table1 cross join table2 where table1. 條件列名 = table2. 條件列名(注:Cross join 后面不能跟 on 只能用 where)
交叉連接(完全)
概念:沒有用 where 子句的交叉連接將產生連接所涉及的笛卡爾積第一個表的行數乘以第二個表的行數等于笛卡爾積和結果集的大小
交叉連接:Cross join(不帶條件 where,如果帶返回或顯示的是匹配的行數)
SQL 語法:
select *from table1 cross join table2
如果有條件(where)
select * from table1 cross join table2 where table1. 條件列名 = table2. 條件列名
等價于
select *from table1,table2 (不帶 where)
到此,關于“SQL 表連接的方法”的學習就結束了,希望能夠解決大家的疑惑。理論與實踐的搭配能更好的幫助大家學習,快去試試吧!若想繼續學習更多相關知識,請繼續關注丸趣 TV 網站,丸趣 TV 小編會繼續努力為大家帶來更多實用的文章!