SQL連接的幾種方式
SQL連接的重要性毋庸置疑,下面就將為您介紹SQL內(nèi)連接和外連接的幾個例子,供您參考,希望對您學習能有所啟迪。
--創(chuàng)建table_A 和table_B兩個表
create table table_A(id int,TextcCol varchar(255))
create table table_B(id int,TextcCol varchar(255))
--插入數(shù)據(jù) 此插入方式是通過union all實現(xiàn)的
insert into table_B
select 2,'hello'
union ALl --此處的union起到連接上下兩個句子的作用
select 3,'world'
union ALl
select 4,'guo'
--內(nèi)連接的兩種方式
--第一種方式:
Select * from table_A,table_B where table_A.id = table_B.id;
--第二種方式:
Select * from table_A A inner join table_B B on A.id = B.id
--外連接 中的左右連接
select * from table_A right join table_B on table_A.id = table_B.id
select * from table_A left join table_B on table_A.id = table_B.id
【編輯推薦】