怎么把多行 的数据归整到一行?

Ritt 2005-12-23 08:46:55
数据库有表 table1
member doings time ---字段名
1 乒乓球 星期一
1 篮球 星期三
2 羽毛球 星期一
2 足球 星期四

我在页面上想显示为
member 星期一 星期二 星期三 星期四 星期五 ...
1 乒乓球 篮球
2 羽毛球 足球

该怎么做啊
请高手指点!
...全文
286 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
Ritt 2005-12-23
  • 打赏
  • 举报
回复
太好了解决了
Ritt 2005-12-23
  • 打赏
  • 举报
回复
谢了先
我试试!
hpym365 2005-12-23
  • 打赏
  • 举报
回复
create table ball
(member int,
doings nvarchar(50),
time nvarchar(10))
insert ball
select 1,'乒乓球','星期1' union all
select 1,'篮球','星期3' union all
select 2,'羽毛球','星期1' union all
select 2,'足球','星期4'
go
select * from ball
go
select member,
星期1=max((case time when '星期1' then doings else '' end)),
星期2=max((case time when '星期2' then doings else '' end)),
星期3=max((case time when '星期3' then doings else '' end)),
星期4=max((case time when '星期4' then doings else '' end)),
星期5=max((case time when '星期5' then doings else '' end)),
星期6=max((case time when '星期6' then doings else '' end)),
星期7=max((case time when '星期7' then doings else '' end))
from ball
group by member
wgsasd311 2005-12-23
  • 打赏
  • 举报
回复
select member
,[星期一]=max(case when [time]='星期一' then doings else '' end)
,[星期二]=max(case when [time]='星期二' then doings else '' end)
,[星期三]=max(case when [time]='星期三' then doings else '' end)
,[星期四]=max(case when [time]='星期四' then doings else '' end)
,[星期五]=max(case when [time]='星期五' then doings else '' end)
,[星期六]=max(case when [time]='星期六' then doings else '' end)
,[星期日]=max(case when [time]='星期日' then doings else '' end)
from tb
group by member

34,873

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server相关内容讨论专区
社区管理员
  • 基础类社区
  • 二月十六
  • 卖水果的net
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧