现在的位置: 首页 > 综合 > 正文

SQl 行变列的实例

2011年01月15日 ⁄ 综合 ⁄ 共 298字 ⁄ 字号 评论关闭

有3个表    Car,User,User_Car 要得到一个用户对应有几种车。

   SQl:

select Uname as 姓名,
MAX(case Name when '宝马' then Smoney end) '宝马',
MAX(case Name when '奔驰' then Smoney end) '奔驰',
MAX(case Name when '大众' then Smoney end) '大众'
from
(select u.Uname,c.Name,uc.Smoney
from dbo.Car c ,dbo.Users u,dbo.User_Car uc
where c.ID = uc.CID and u.ID=uc.[UID]) aa
group by aa.Uname

抱歉!评论已关闭.