inner join (內連線)
outer join (外連線)
cross join
natural join
顯式:
use
database
;select
*from table_1 t1
join table_2 t2 on t1.id = t2.id;
隱式: (堅決不用)
use
database
;select
*from table_1 t1, table_2 t2
where t1.id = t2.id;
use
database
;select
*from table_1 t1
join table_2 t2 on t1.id = t2.id and t1.product_id = t2.product_id;
當所選關鍵列標籤名相同,可以使用using來簡化語法。
use
database
;select
*from table_1 t1
join table_2 t2 using
(id)
;
可以使得兩個子查詢合併結果。
use sql_store;
select
order_id,
order_date,
'active'
asstatus
from orders
where order_date >=
'2019-01-01'
union
select
order_id,
order_date,
'archived'
asstatus
from orders
where order_date <
'2019-01-01'
;
use
database
;select
*from table_1 t1
join table_2 t2 on t1.id = t2.id;
use sql_hr;
select
*from employees e
join employees m on e.reports_to = m.employee_id;
use database_1;
select
*from table_1 t1
join database_2.table_2 t2 on t1.id = t2.id;
use
database
;select
*from table_1 t1
join table_2 t2 on t1.id = t2.id
join table_3 t3 on t2.id = t3.id;
左連線的表不論是否滿足條件,都會返回結果,即保證左邊一定全部輸出,右邊若無則輸出null。
use
database
;select
*from table_1 t1
left
join table_2 t2 on t1.id = t2.id;
右連線,同理,但是堅決不用,本質上和內連線實現的功能一樣。
use
database
;select
*from table_1 t1
right
join table_2 t2 on t1.id = t2.id;
use sql_hr;
select
*from employees e
left
join employees m on e.reports_to = m.employee_id;
實現第乙個表與第二個表的排列組合,通常會進行排序。
use
database
;select
*from table_1 t1
cross
join table_2 t2 on t1.id = t2.id
order
by t1.name;
簡短精煉,但堅決不用。
use
database
;select
*from table_1 t1
natural
join table_2 t2;
MySql 學習筆記 連線表(JOIN)
inner join內連線 將乙個表中的行與其他表中的行進行匹配 select column list from t1inner join t2 on join condition1 inner join t3 on join condition2 where where conditions 由於...
MySQL學習筆記 連線
a table a b table b 1 笛卡爾積 為a,b兩個表產生笛卡爾積 x y x y 的笛卡爾積為,個人理解 即將a表所有的屬性和b表中的所有屬性進行合併,且每條記錄都產生乙個這樣的關係。select from a cross join b orselect from a,b2 自然連線...
SQLSERVER各種表連線
2.1常用表連線 inner join,left join,right join,full join,cross join if object id n table1 n u is not null drop table table1 if object id n table2 n u is not...