这个合并查询要怎么写?

steven7677 2009-03-09 10:36:20
userID exYear
1 2007
2 2007
1 2008

现在想查询2007~2009年的情况,形成下表
userID exYear
1 2009
1 2008
1 2007
2 2009
2 2008
2 2007
...全文
130 11 打赏 收藏 转发到动态 举报
写回复
用AI写文章
11 条回复
切换为时间正序
请发表友善的回复…
发表回复
syw_java 2009-12-22
  • 打赏
  • 举报
回复
怎么像是拼出来的
ACMAIN_CHM 2009-12-21
  • 打赏
  • 举报
回复
select distinct userID,b.xy as exYear
from userID,(select 2007 as xy union all select 2008 as xy union all select 2009 as xy) b
rucypli 2009-03-09
  • 打赏
  • 举报
回复
select userid,exYear
from tb
group by userid,exYear
order by userid,exYear
claro 2009-03-09
  • 打赏
  • 举报
回复
select * from (select 1 userid union all select 2) b ,
(select 2009 as exYear union all select 2008 union all select 2007) bb
userid exYear
1 2009
1 2008
1 2007
2 2009
2 2008
2 2007
(6 行受影响)

数据好弄,需求应该不是这样。
claro 2009-03-09
  • 打赏
  • 举报
回复
帮顶
已有贴回复类似问题。
htl258_Tony 2009-03-09
  • 打赏
  • 举报
回复
慢老大一点
dawugui 2009-03-09
  • 打赏
  • 举报
回复
create table tb(userID int,exYear int)
insert into tb values(1 , 2007 )
insert into tb values(2 , 2007 )
insert into tb values(1 , 2008 )
go

select m.userid , n.exyear from
(select distinct userID from tb) m,
(select 2009 as exYear union all select 2008 union all select 2007) n

drop table tb

/*
userid exyear
----------- -----------
1 2009
1 2008
1 2007
2 2009
2 2008
2 2007

(所影响的行数为 6 行)
*/
快乐_石头 2009-03-09
  • 打赏
  • 举报
回复
ORDER BY userID, exYear  DESC
htl258_Tony 2009-03-09
  • 打赏
  • 举报
回复
select a.userid,b.exyear
from (select distinct userid from tb) a,
(select exyear=2007 union select 2008 union select 2009) b
dawugui 2009-03-09
  • 打赏
  • 举报
回复
select m.userid , n.exyear from
(select distinct userID from tb) m,
(select 2009 as exYear union all select 2008 union all 2007) n
闽之东 2009-03-09
  • 打赏
  • 举报
回复
select UserID from 表名 where exYear in (2007,2008,2009) order by UserID

34,587

社区成员

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

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