27,579
社区成员
发帖
与我相关
我的任务
分享
declare @sell table (id int,huohao varchar(10),price int,id_huiyuan int)
insert into @sell
select 1,'0001',30,12 union all
select 1,'0002',22,13
declare @huiyuan table (id int,name varchar(10),id_zu int)
insert into @huiyuan
select 12,'xxx',1 union all
select 13,'ccc',2
declare @zu_huiyuan table (id int,zu varchar(10))
insert into @zu_huiyuan
select 1,'行政部' union all
select 2,'操作部'
select a.*,b.name,c.zu from @sell a , @huiyuan b, @zu_huiyuan c where a.id_huiyuan=b.id
and b.id_zu=c.id
select a.*,b.name,c.zu from @sell a join @huiyuan b on a.id_huiyuan=b.id
join @zu_huiyuan c on b.id_zu=c.id
select a.*,b.name,c.zu from @sell a inner join @huiyuan b on a.id_huiyuan=b.id
inner join @zu_huiyuan c on b.id_zu=c.id
select a.*,b.name,c.zu from @sell a left join @huiyuan b on a.id_huiyuan=b.id
left join @zu_huiyuan c on b.id_zu=c.id
select a.*,b.name,c.zu from @sell a left outer join @huiyuan b on a.id_huiyuan=b.id
left outer join @zu_huiyuan c on b.id_zu=c.id
/*结果
id huohao price id_huiyuan name zu
-------------------------------------------------
1 0001 30 12 xxx 行政部
1 0002 22 13 ccc 操作部
*/
select a.id,a.huohao,a.price,b.name,c.zu
from sell a,huiyuan b,zu_huiyuan c
where a.id_huiyuan=b.id and b.Id_zu=c.id
select a.id,a.huohao,a.price,b.name,c.zu
from sell a inner join huiyuan b
on a.id_huiyuan=b.id
inner join zu_huiyuan c
on b.Id_zu=c.id
两种写法都是一样的
select a.id,a.huohao,a.price,b.name,c.zu
from sell a
left join huiyuan b
on a.id_huiyuan=b.id
left join zu_huiyuan c
on b.id_zu=c.id
--左连接
select a.id,a.huohao,a.price,b.name,c.zu
from sell a
left outer join huiyuan b
on a.id_huiyuan=b.id
left outer join zu_huiyuan c
on b.id_zu=c.id
--内连接
select a.id,a.huohao,a.price,b.name,c.zu
from sell a
join huiyuan b
on a.id_huiyuan=b.id
join zu_huiyuan c
on b.id_zu=c.id
select s.id,huohao,price,name,zu from sell s inner join huiyuan h
on s.id=h.id
inner join zu_huiyuan z
on h.id=z.id
select a.id,a.huohao,a.price,b.name,c.zu
from sell a
join huiyuan b
on a.id_huiyuan=b.id
join zu_huiyuan c
on b.id_zu=c.id
select * into #temp from tablename