資料庫中表的連線方式詳解

2021-07-24 15:56:03 字數 1234 閱讀 2544

create table a (id number(3),name varchar2(20));

create table b (id number(3),name varchar2(50));

insert  into a values(1,'小一');

insert  into a values(2,'小二');

insert  into a values(3,'小三');

insert into b values(1,'小小');

insert into b values(2,'小中');

insert into b values(4,'小大');

insert into b values(5,'大大');

--連線分為兩類:1、內連線  2、外連線(左外連線、右外連線、全外連線(只要有資料,不匹配的表字段補null))3、笛卡爾集(交叉連線)

--以下第一條為資料庫中通用寫法,第二條為oracle資料庫特有:

--內連線:(對於不匹配的都會進行捨棄)

select * from a inner join b on a.id=b.id;

select * from a,b where a.id = b.id;

--外連線:(對於不匹配的字段補null)

--左外連線:(以左表為基表(驅動表),將左表的每一條資料都與右表匹配,如果在右表中沒有匹配資料,則右表補null)

select * from a left join b on a.id=b.id;

select * from a,b where a.id=b.id(+);

--右外連線:(以右表為基表(驅動表),將右表的每一條資料都與左表匹配,如果在左表中沒有匹配資料,則左表補null)

select * from a right join b on a.id = b.id;

select * from a,b where a.id(+) = b.id;

--全外連線:左表和右表沒有符合條件的都補null值

select * from a full join b on a.id = b.id;

select * from a,b where a.id(+)=b.id union select * from a,b where a.id = b.id(+);

--交叉連線:(全連線,笛卡爾集,全排列)

select * from a cross join b;

select * from a,b;

資料庫中表的連線方式詳解

create table a id number 3 name varchar2 20 create table b id number 3 name varchar2 50 insert into a values 1,小一 insert into a values 2,小二 insert int...

Oracle資料庫中表的兩種連線方式

對oracle資料庫中表的兩種連線方式的詳細解析,在oracle資料庫中表的一共有四種連線方式,但是本文主要講述的前兩種,希望大家在瀏覽完以下的文章會對oracle資料庫中表的連線方式有所了解。表的連線是指在乙個sql語句中通過表與表之間的關連,從乙個或多個表中檢索相關的資料,大體上表與表之間的連線...

資料庫中表的連線(多關係連線查詢)

資料庫中表的連線 多關係連線查詢 用的最多的是內連線 在連線的兩個表中,只有滿足連線條件的元祖,才作為結果輸出。例 a表 b表 sql語句 select a.id,a.name,a.gender,b.id,b.no,b.class,b.grade from a inner join b on a.i...