create table table1(id int,name int) insert into table1 select 1,1 union select 2,2 union select 3,3 union select 4,4 create table table2(id int,name int) insert into table2 select 1,3 union select 2,4 union select 6,4 select * from table1 inner join table2 on table1.id=table2.id go select * from table1,table2 where table1.id=table2.id go drop table table1 drop table table2 go
create table table1(id int,name int)
insert into table1 select 1,1
union select 2,2
union select 3,3
union select 4,4
create table table2(id int,name int)
insert into table2
select 1,3
union select 2,4
union select 6,4
select *
from table1 inner join table2 on table1.id=table2.id
go
select *
from table1,table2
where table1.id=table2.id
go
drop table table1
drop table table2
go
完全外联包含两张表的所有记录.
左联是以左边的表为主,右边的为辅,右联则相反4.一般要使得数据库查询语句性能好点遵循一下原则: * 在做表与表的连接查询时,大表在前,小表在
* 不使用表别名,通过字段前缀区分不同表中的字段
* 查询条件中的限制条件要写在表连接条件前
* 尽量使用索引的字段做为查询条件
我的習慣是,JOIN步驟用ON篩選器并用新語法,特別對于INNER及OUTER.