首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 数据库 > SQL Server >

SQL求一视图,行连接列~

2012-03-25 
SQL求一视图,行连接列~~~~~~~~~在线等``表AidBrandIDtypeID114235表BIDNAME1DELL2联想3AMD4内存5硬盘想要

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

热点排行