oracle的连接查询(内外连接)

来源:互联网 发布:linux命令强制删除文件 编辑:程序博客网 时间:2024/05/17 21:38
-- 连接查询的分类
-- 1.内连接:又叫做等值连接。
-- 2.外连接:又叫做左连接、右连接、全连接。

现已 commodity_table 和 store 这两张表 做示例;
store 表中的外键是 community_number,与之相关联的是commodity_table 表的主键 serial_number 。



例:-- 内连接(简写)
select a.name,b.store_name from commodity_table a,store b wherea.serial_number=b.commodity_number;

-- 内连接(完整的写法)
select a.name,b.store_name form from commodity_table ainner join store b on a.serial_number=b.commodity_number;


-- 外连接-左连接(以左表为基准,关联右表,关联上的数据直接显示,
-- 未关联上的则显示左表记录,右表记录为空值
select * from commodity_table aleft join store b on a.serial_number=b.commodity_number;
--外连接-左连接的另一种简写方法
select * from commodity_table a,store bwhere a.serial_number=b.commodity_number(+);


-- 外连接-右连接(以右表为基准,关联左表,关联上的数据直接显示,
-- 未关联上的则显示右表记录,左表记录为空值
select * from commodity_table a right join store bon a.serial_number=b.commodity_number;
--外连接-右连接的另一种简写方法
select * from commodity_table a,store b wherea.serial_number(+)=b.commodity_number;


-- 外连接-全连接
select * from commodity_table afull join store b on a.serial_number=b.commodity_number
order by serial_number;

-- 等价于 左连接跟右连接的集合
select * from commodity_table a left join store bon a.serial_number=b.commodity_number
union
select * from commodity_table aright join store b on a.serial_number=b.commodity_number;

0 0
原创粉丝点击