来源:http://www.3lian.com/edu/2013/05-14/69869.html
联合查询的效率比较高,一般分为以下几种:inner join (内联)、left outer join(左联查)、right outer join(右联查)、full outer join(全联查)。
create table T1(
userid int,
username varchar(20),
passwd varchar(20)
)
insert into T1 values(1,'jack','jackpd');--实现单行插入
insert into T1 values(2,'bob','bobpd');
commit
create table T2(
userid int,
counts varchar(20),
grade varchar(20)
)
select * from T2 for update;
1、内联:把两个表中所有符合条件的行
- select * from T1,T2 where T1.userid=T2.userid;
- select * from T1 inner join T2 on T1.USERID=T2.USERID; 这种方法比前面的方法效率高
2、左联:显示左表所有的行,右表中符合条件的行加到左表的右边显示,不符合条件的行显示空值
- select * from T1 left join T2 on T1.USERID=T2.USERID
3、右联:与左联情况相反
- select * from T1 right join T2 on T1.USERID=T2.USERID;
4、全联:显示走左和右表所有的行(即左联+右联的结果),然后过滤掉重复的行
- select * from T1 full join T2 on T1.USERID=T2.USERID;