1. 程式人生 > >Basic Join 基本交,Cross Join 叉交,INNER JOIN 內交,OUTER JOIN 外交,Left join 左交,Right join 右交,Full join全交都是什麼?

Basic Join 基本交,Cross Join 叉交,INNER JOIN 內交,OUTER JOIN 外交,Left join 左交,Right join 右交,Full join全交都是什麼?

Basic Join Operation--comma-separated join(叉乘,都乘到N*M,N,M分別是兩個表的條數)
馬克-to-win:  select * from register, student;

Cross Join(叉乘,都乘到N*M,N,M分別是兩個表的條數)---same as comma-separated join

select * from register CROSS JOIN student;

select r.name, s.name,s.age from register r CROSS JOIN student s where r.id = s.id AND s.age >20;

INNER JOIN

交叉聯接的作用同內聯接一樣。例如,下面的 Transact-SQL 查詢得到相同的結果集: 

select * from register INNER JOIN student;

select r.name, s.name,s.age from register r INNER JOIN student s on r.id = s.id AND s.age >20;

OUTER JOIN
Left join
Return all matched rows and all unmatched rows from the left table
Right join
Return all matched rows and all unmatched rows from the right table
Full join
Return all matched and unmatched rows from both tables

select * from register as r left join student as s on r.id = s.id;
select * from register as r right join student as s on r.id = s.id;
select * from register as r full join student as s on r.id = s.id;(mysql不支援full join)

比較:inner join,cross join,join只是找回所有符合條件的行,不基於誰,不返回null。但外交不一樣, Left join時,左表全回來,右表用null補上,見目錄裡的例子。