数据库左右连接(分享)
2012-08-21 17:26
155 查看
--建立测试数据
createtable a(id number);
createtable b(id number);
insertinto a values(1);
insertinto a values(2);
insertinto a values(3);
insertinto b values(1);
insertinto b values(2);
insertinto b values(4);
commit;
--左:
--主流数据库通用的方法
select * from a leftjoin b on a.id=b.id;
--Oracle特有的方法
select * from a, b where a.id=b.id(+);
ID ID
---------- ----------
1 1
2 2
3
--右:
--主流数据库通用的方法
select * from a rightjoin b on a.id=b.id;
--Oracle特有的方法
select * from a, b where a.id(+)=b.id;
ID ID
---------- ----------
1 1
2 2
4
--内
--主流数据库通用的方法
select * from a join b on a.id=b.id;
--where关联
select * from a, b where a.id=b.id;
ID ID
---------- ----------
1 1
2 2
--全外
--主流数据库通用的方法
select * from a fulljoin b on a.id=b.id;
--Oracle特有的方法
select *
from a, b
where a.id = b.id(+)
union
select *
from a, b
where a.id(+) = b.id;
ID ID
---------- ----------
1 1
2 2
3
4
--完全,也叫交叉连接或者笛卡尔积
--主流数据库通用的方法
select * from a,b;
--或者
select * from a crossjoin b;
ID ID
---------- ----------
1 1
1 2
1 4
2 1
2 2
2 4
3 1
3 2
3 4
连接无非是这几个
--内连接和where相同
innerjoin
--左向外连接,返回左边表所有符合条件的
leftjoin
--右向外连接,返回右边表所有符合条件的
rightjoin
--完整外部连接,左向外连接和右向外连接的合集
fulljoin
--交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合
crossjoin
--补充:
--左向外连接,返回左边表所有符合条件的,
--注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录
select *
from a, b
where a.id = b.id(+)
and b.id = 2;
ID ID
---------- ----------
2 2
--左向外连接,返回左边表所有符合条件的
--注意where上第二个加号,它的作用是修改右边表记录的显示,例如如果b.id(+) = 2,显示为2,否则显示null
select *
from a, b
where a.id = b.id(+)
and b.id(+) = 2;
ID ID
---------- ----------
2 2
3
1
createtable a(id number);
createtable b(id number);
insertinto a values(1);
insertinto a values(2);
insertinto a values(3);
insertinto b values(1);
insertinto b values(2);
insertinto b values(4);
commit;
--左:
--主流数据库通用的方法
select * from a leftjoin b on a.id=b.id;
--Oracle特有的方法
select * from a, b where a.id=b.id(+);
ID ID
---------- ----------
1 1
2 2
3
--右:
--主流数据库通用的方法
select * from a rightjoin b on a.id=b.id;
--Oracle特有的方法
select * from a, b where a.id(+)=b.id;
ID ID
---------- ----------
1 1
2 2
4
--内
--主流数据库通用的方法
select * from a join b on a.id=b.id;
--where关联
select * from a, b where a.id=b.id;
ID ID
---------- ----------
1 1
2 2
--全外
--主流数据库通用的方法
select * from a fulljoin b on a.id=b.id;
--Oracle特有的方法
select *
from a, b
where a.id = b.id(+)
union
select *
from a, b
where a.id(+) = b.id;
ID ID
---------- ----------
1 1
2 2
3
4
--完全,也叫交叉连接或者笛卡尔积
--主流数据库通用的方法
select * from a,b;
--或者
select * from a crossjoin b;
ID ID
---------- ----------
1 1
1 2
1 4
2 1
2 2
2 4
3 1
3 2
3 4
连接无非是这几个
--内连接和where相同
innerjoin
--左向外连接,返回左边表所有符合条件的
leftjoin
--右向外连接,返回右边表所有符合条件的
rightjoin
--完整外部连接,左向外连接和右向外连接的合集
fulljoin
--交叉连接,也称笛卡儿积。返回左表中的每一行与右表中所有行的组合
crossjoin
--补充:
--左向外连接,返回左边表所有符合条件的,
--注意这里没有第二个加号,会直接过滤掉数据,只显示符合条件的记录
select *
from a, b
where a.id = b.id(+)
and b.id = 2;
ID ID
---------- ----------
2 2
--左向外连接,返回左边表所有符合条件的
--注意where上第二个加号,它的作用是修改右边表记录的显示,例如如果b.id(+) = 2,显示为2,否则显示null
select *
from a, b
where a.id = b.id(+)
and b.id(+) = 2;
ID ID
---------- ----------
2 2
3
1
相关文章推荐
- C#提升性能"数据库连接打开与关闭"经验分享(附:优化过的DBHelper类) 之配餐系统的开发
- 分享一下你喜欢怎么连接数据库
- 数据库左右连接
- php连接不同数据库的方法分享
- 数据库左右内外连接
- java连接orcale数据库示例分享
- SHOUG线上活动 Maclean Liu分享《学习甲骨文数据库的自由之翼-与Oracle的世界相连接》
- asp.net连接数据库读取数据示例分享
- C#2005用XML来保存连接数据库的字符串(这样改变了服务器连接配制只需改一下这个文件就成了),琢磨了几天终于有解了,和大家分享一下,希望高手指点!
- [分享] 数据库连接配置管理组件ConnectionManager (Full Source)
- 数据库中左右连接
- java连接数据库(代码分享)
- 【数据库】左右连接和内连接
- 即将我就不再学习Java课程,在这最后之际,分享一下我jdbc连接数据库的看法
- 数据库左右(left,right)连接及多表连接(inner)
- Android连接数据库之服务器端调试源码分享
- 数据库左右连接
- C#提升性能"数据库连接打开与关闭"经验分享(附:优化过的DBHelper类) 之配餐系统的开发
- java数据库操作类演示实例分享(java连接数据库)
- 浅析数据库中内连接、全连接与左右连接的不同点