小弟初学SQL有个问题搞不明白,比如有两个表
----------tb1
id name
1 李
2 王
3 张
----------tb2
id name id2 class
1 校长 1 1
2 主任 1 2
3 老师 1 3
4 主任 2 2
5 老师 2 3
6 老师 3 3
----------我是这样写的,总之得到的数据不对
select a.name,b.name,c.name,d.name,
from tb1 a
left join tb2 b on a.id = b.id2 and class = 1
left join tb2 c on a.id = c.id2 and class = 2
left join tb2 d on a.id = d.id2 and class = 3
----------我想得到这样的结果
a.name b.name c.name d.name
李 校长 主任 老师
王 NULL 主任 老师
张 NULL NULL 老师----------请问怎么弄啊,还有为什么我的想法不对啊,总是取出重复的数据,小弟在线等,帮忙啊
----------tb1
id name
1 李
2 王
3 张
----------tb2
id name id2 class
1 校长 1 1
2 主任 1 2
3 老师 1 3
4 主任 2 2
5 老师 2 3
6 老师 3 3
----------我是这样写的,总之得到的数据不对
select a.name,b.name,c.name,d.name,
from tb1 a
left join tb2 b on a.id = b.id2 and class = 1
left join tb2 c on a.id = c.id2 and class = 2
left join tb2 d on a.id = d.id2 and class = 3
----------我想得到这样的结果
a.name b.name c.name d.name
李 校长 主任 老师
王 NULL 主任 老师
张 NULL NULL 老师----------请问怎么弄啊,还有为什么我的想法不对啊,总是取出重复的数据,小弟在线等,帮忙啊
-- Author :fredrickhu(小F,向高手学习)
-- Date :2011-03-25 17:11:08
-- Verstion:
-- Microsoft SQL Server 2008 (RTM) - 10.0.1600.22 (Intel X86)
-- Jul 9 2008 14:43:34
-- Copyright (c) 1988-2008 Microsoft Corporation
-- Enterprise Edition on Windows NT 5.2 <X86> (Build 3790: Service Pack 2)
--
----------------------------------------------------------------
--> 测试数据:[tb1]
if object_id('[tb1]') is not null drop table [tb1]
go
create table [tb1]([id] int,[name] varchar(2))
insert [tb1]
select 1,'李' union all
select 2,'王' union all
select 3,'张'
--> 测试数据:[tb2]
if object_id('[tb2]') is not null drop table [tb2]
go
create table [tb2]([id] int,[name] varchar(4),[id2] int,[class] int)
insert [tb2]
select 1,'校长',1,1 union all
select 2,'主任',1,2 union all
select 3,'老师',1,3 union all
select 4,'主任',2,2 union all
select 5,'老师',2,3 union all
select 6,'老师',3,3
--------------开始查询--------------------------
select
a.name,b.name,c.name,d.name
from
tb1 a
left join tb2 b on a.id = b.id2 and b.class = 1
left join tb2 c on a.id = c.id2 and c.class = 2
left join tb2 d on a.id = d.id2 and d.class = 3----------------结果----------------------------
/* name name name name
---- ---- ---- ----
李 校长 主任 老师
王 NULL 主任 老师
张 NULL NULL 老师(3 行受影响)*/
ona.id=b.id2
这俩有重复数据
----------tb1
id name
1 A大学
2 B大学
3 C大学
----------tb2
id tb1id tb3id
1 1 1
2 1 2
3 1 3
4 2 2
5 2 3
6 3 3
---------tb3
id name class
1 校长 1
2 主任 2
3 老师 3
4 主任 2
5 老师 3
6 老师 3
----------我是这样写
select a.name,b.name,c.name,dname
from tb1 a
left join tb2 b on a.id = b.tb1id
left join tb3 c on b.tb3id = c.id
left join tb3 d on b.tb3id = d.id
left join tb3 e on b.tb3id = e.id
----------我得到的结果是
A大学 校长 NULL NULL
A大学 NULL 主任 NULL
A大学 NULL NULL 老师
B大学 NULL 主任 NULL
B大学 NULL NULL 老师
C大学 NULL NULL 老师
-----我想要的结果是
name name1 name2 name3
A大学 校长 主任 老师
B大学 NULL 主任 老师
C大学 NULL NULL 老师
------------------------请问我哪里有错误,或者我的想法对吗?
from () t
froup by t.a
from tb1 a
join tb2 b on a.id = b.tb1id
join tb3 c on b.tb3id = c.id
join tb3 d on b.tb3id = d.id
join tb3 e on b.tb3id = e.id
group by a.name
select aname,max(cname),max(dname),max(ename)
from (select a.name aname,c.name cname,d.name dname,e.name ename
tb1 a
join tb2 b on a.id = b.tb1id
join tb3 c on b.tb3id = c.id
join tb3 d on b.tb3id = d.id
join tb3 e on b.tb3id = e.id)t
group by aname
就是想找一个更简单的方法
如果我的结果里就是有两个一样的名称呢
我的name里就是有很多重复的
tb2是关系表
黑龙江工程tb1
楼房tb3
电房tb3
黑龙江工程tb1
机组tb3
道路tb3
如果要是用了group by aname 会不会将我的第二个name过滤掉了呢?
from tb1 a
left join tb2 b on a.id = b.tb1id
left join tb3 c on b.tb3id = c.id这个结果再行列转换下就可以了。
select a.name,b.name,c.name,d.name
from tb1 a
left join tb2 b on a.id = b.id2 and b.class = 1
left join tb2 c on a.id = c.id2 and c.class = 2
left join tb2 d on a.id = d.id2 and d.class = 3
改成這樣就沒問題了啊。。