参见:http://zhidao.baidu.com/link?url=RSxjM9KITrGBB6KAAj484P2xUV-_6eHy14jkk6hozksrux8g6k2Kc2Oz2lJpTzC7PppFRNlYcY1U1hh6ZkhOCK
数据库连接查询分两种:内连接查询和外连接查询。而外连接查询可分为左外连接查询、右外连接查询和全外连接查询。
内连接查询
只显示两表之间关联条件匹配的数据。
select 字段列表 from 表1 join 表2 on 两个表之间的关联条件
select 字段列表 from 表1,表2 where 两个表之间的关联条件
完整写法:select 字段列表 from 表1 inner join 表2 on 两个表之间的关联条件
外连接查询
full outer 全外连接 两表之间关联条件匹配的数据,A表有但B表没有的数据(显示null),B表有A表没有的数据(显示null),都显示。
A表 left join B表 左外连接 以A表为主表,A表的全部数据,B表有匹配关系的就组合,没有的为null
A表 right join B表 右外连接 以B表为主表,B表的全部数据,A表有匹配关系的就组合,没有的为null
A表
id name
1 lee
2
zhang
4 wang
B表
id score
1 90
2
100
3 70
左外连接查询:select * from A left join B on A.id=B.id;
id name id score
1 lee 1 90
2 zhang 2 100
4 wang null null
右外连接查询:select * from A right join B on A.id=B.id;
id name id score
1 lee 1 90
2 zhang 2 100
null null 3 70
全外连接查询:select * from A full join B on A.id=B.id;
id name id score
1 lee 1 90
2 zhang 2 100
4 wang null null
null null 3 70
内连接查询:select * from A join B on A.id=B.id;
id name id score
1 lee 1 90
2 zhang 2 100
交叉连接查询:select * from A cross join B <==>select * from A,B
id name id score
1 lee 1 90
2 zhang 1 90
4 wang 1 90
1 lee 2 100
2 zhang 2 100
4 wang 2 100
1 lee 3 70
2 zhang 3 70
4 wang 3 70
本站仅提供存储服务,所有内容均由用户发布,如发现有害或侵权内容,请
点击举报。