inner join 内连接: Select a.*,b.* From tbA a inner join tbB b On a.ID=b.ID 等同于 Select a.*,b.* From tb a,tb b Where a.ID=b.ID;left join 左链接: Select a.*,b.* From tbA a Left Join tbB b On a.ID=b.ID 输出结果为: a表的所有内容和b表的内容(如果在a.ID=b.ID条件下,b标有对应的内容,那么,返回b.*;否则b.*的所有字段对应于a的当前记录全部置为null);case ... when ... then : if ... then ... 语句 例如:case FieldA when '1' then 'Hello world' 换成if ... then ... 语句: if FieldA='1' then 'Hello world'
数据可以模拟如下: A ID Dictkey 1 1 2 3 2B ID Dictkey 1 1 2 2如果使用 1. select a.id,a.dictkey from a,b where a.dictkey=b.dictkey 那么结果集如下: a.id a.dictkey 1 1 3 22. select a.id,a.dictkey from a,b where a.dictkey=b.dictkey(+) 那么结果集如下: a.id a.dictkey 1 1 2 3 2
例如有以下的2个表ta,tb select * from ta A B C D 1 0 0 0 0 2 1 1 1 1 3 2 2 2 2 4 3 3 3 3 5 4 4 4 4 select * from tb A C D E 1 0 0 0 0 2 1 1 1 1 3 2 2 1 2 4 3 3 3 3 5 2 4 1 4 left join: select ta.*,tb.* from ta left join tb on tb.a = ta.a A B C D A C D E 1 0 0 0 0 0 0 0 0 2 1 1 1 1 1 1 1 1 3 2 2 2 2 2 2 1 2 4 3 3 3 3 3 3 3 3 5 2 2 2 2 2 4 1 4 6 4 4 4 4 right join: select ta.*,tb.* from ta right join tb on tb.a = ta.a A B C D A C D E 1 0 0 0 0 0 0 0 0 2 1 1 1 1 1 1 1 1 3 2 2 2 2 2 4 1 4 4 2 2 2 2 2 2 1 2 5 3 3 3 3 3 3 3 3 inner join: select ta.*,tb.* from ta inner join tb on tb.a = ta.a A B C D A C D E 1 0 0 0 0 0 0 0 0 2 1 1 1 1 1 1 1 1 4 2 2 2 2 2 4 1 4 3 2 2 2 2 2 2 1 2 5 3 3 3 3 3 3 3 3 select ta.*,tb.* from ta inner join tb on tb.a = ta.a and tb.c=ta.c A B C D A C D E 1 0 0 0 0 0 0 0 0 2 1 1 1 1 1 1 1 1 3 2 2 2 2 2 2 1 2 4 3 3 3 3 3 3 3 3 这样应该很清楚了吧
前者内连接,后者外连接中的左连接
内连接时,必须关联字段两边都有值
左连接时,右边字段可以为NULL
case ... when ... then 就像switch case 一样的语法,当满足什么条件时,取什么值的意思
=>这个应该是标准SQL中使用,以前使用SQL SERVER有用过,不过改用ORACLE后再也没有用过这个语法。
假设有业务数据A表,数据字典B表
A表中的DICYKEY(可能为空),引用B表的DICTKEY,那么
要体现A表中的那么为空的数据时就要用
A.DICTKEY=B.DICTKEY(+)
如果使用等值关联A.DICTKEY=B.DICTKEY,那么查询出来的结果集,为空的部分数据就体现不出来。
select ... case...when...then...when...then...是什么意思?
=>如1楼回复,去看些基础语法是有帮助的。
Select a.*,b.* From tbA a inner join tbB b On a.ID=b.ID 等同于 Select a.*,b.* From tb a,tb b Where a.ID=b.ID;left join 左链接:
Select a.*,b.* From tbA a Left Join tbB b On a.ID=b.ID 输出结果为:
a表的所有内容和b表的内容(如果在a.ID=b.ID条件下,b标有对应的内容,那么,返回b.*;否则b.*的所有字段对应于a的当前记录全部置为null);case ... when ... then : if ... then ... 语句
例如:case FieldA when '1' then 'Hello world'
换成if ... then ... 语句: if FieldA='1' then 'Hello world'
A
ID Dictkey
1 1
2
3 2B
ID Dictkey
1 1
2 2如果使用
1.
select a.id,a.dictkey from a,b where a.dictkey=b.dictkey
那么结果集如下:
a.id a.dictkey
1 1
3 22.
select a.id,a.dictkey from a,b where a.dictkey=b.dictkey(+)
那么结果集如下:
a.id a.dictkey
1 1
2
3 2
select * from ta
A B C D
1 0 0 0 0
2 1 1 1 1
3 2 2 2 2
4 3 3 3 3
5 4 4 4 4
select * from tb
A C D E
1 0 0 0 0
2 1 1 1 1
3 2 2 1 2
4 3 3 3 3
5 2 4 1 4
left join: select ta.*,tb.* from ta left join tb on tb.a = ta.a
A B C D A C D E
1 0 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1 1
3 2 2 2 2 2 2 1 2
4 3 3 3 3 3 3 3 3
5 2 2 2 2 2 4 1 4
6 4 4 4 4
right join: select ta.*,tb.* from ta right join tb on tb.a = ta.a
A B C D A C D E
1 0 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1 1
3 2 2 2 2 2 4 1 4
4 2 2 2 2 2 2 1 2
5 3 3 3 3 3 3 3 3
inner join:
select ta.*,tb.* from ta inner join tb on tb.a = ta.a
A B C D A C D E
1 0 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1 1
4 2 2 2 2 2 4 1 4
3 2 2 2 2 2 2 1 2
5 3 3 3 3 3 3 3 3
select ta.*,tb.* from ta inner join tb on tb.a = ta.a and tb.c=ta.c
A B C D A C D E
1 0 0 0 0 0 0 0 0
2 1 1 1 1 1 1 1 1
3 2 2 2 2 2 2 1 2
4 3 3 3 3 3 3 3 3
这样应该很清楚了吧
那么inner join和join又有什么区别和联系呢?
我只懂一点Oracle。新进的公司要求做Web开发,后台是Oracle数据流,工作真是难做!
left outer join
right outer joininner和outer可以省略