读书人

SQL求一视图,行连接列~

发布时间: 2012-03-25 20:55:17 作者: rapoo

SQL求一视图,行连接列~~~~~~~~~在线等``
表 A

id BrandID typeID
1 1 4
2 3 5
表B

ID NAME
1 DELL
2 联想
3 AMD
4 内存
5 硬盘
想要结果

1 DELL 内存
2 AMD 硬盘

就是把表A中 BrandID typeID列的值用表B中对应的NAME换掉.

[解决办法]
select o.ID,q.Name,q.Name from A o,B p,B q where o.BrandID=p.ID and o.typeID=q.ID
[解决办法]

SELECT A.id, BrandName = B1.NAME, typeName = B2.Name
FROM A, B B1, B B2
WHERE A.BrandID = B.ID
AND A.typeID = B.ID

[解决办法]
select a.id,b1.name as Brand,b2.name as type
from a,b b1,b b2
where a.BrandID=b1.BrandID and
a.typeID=b2.typeID
[解决办法]
select
A.ID,
max(case A.BrandID when B.ID then B.Name end),
max(case A.typeID when B.ID then B.Name end)
from
A,B
group by
A.ID,A.BrandID,A.typeID

读书人网 >SQL Server

热点推荐