数据库多表连接查询

多表查询定义:根据不同的连接条件将一个表中的不同数据连接起来

多表查询的语法:

select table1.a ,table2.b from table1,table2 where table1.ID=table2.ID

简写:利用表名别名的方式来简写

select A.a,B.b from table1 A,table2 B

where A.ID=B.ID

利用表的别名或者表的前缀,使用表的别名可以简化查询,使用表的前缀可以提供查询性能

多表连接的类型:内连接和外连接

内连接:只返回满足数据连接条件的数据

外连接:除了返回满足条件的数据之外还返回左(右)两个表不满足条件的数据,又称为左(右)连接;

内连接语法:

select table1.a,table2.b from table1 join table2 on (table1.ID=table2.ID)

外连接可以查询另一个表或者两个中不满足条件的数据。外连接的符号是(+) 要放在字段的后面,(+)对面的那个表的内容会全部显示出来

外连接语法:

select table1.a,table2.b from table1,table2 where table1.ID(+)=table2.ID    ---右外连接

select table1.a,table2.b from table1,table2 where table1.ID=table2.ID(+)    ---左外连接

 

右连接:

select table1.a,table2.b from table1 right join table2 on (table1.ID=table2.ID)

左连接:

select table1.a,table2.b from table1 left join table2 on (table1.ID=table2.ID)

满连接:

select table1.a,table2.b from table1 full join table2 on (table1.ID=table2.ID)

 

 

 

posted @ 2013-05-08 13:03  Mr.liub  阅读(302)  评论(0编辑  收藏  举报