请问如何将该查询结果横向显示

greenberet007 武汉起点人力资源有限公司 Web前端工程师  2008-04-02 04:31:16
请问如何将该查询结果横向显示

ID Title Icon
691 tes 2008/3/29/167d7062-ca9e-4ddb-ac68-a72554308187.jpg
690 Analog 2008/3/29/3fc213fd-971d-4017-863a-b4e395217ca8.jpg
689 dfdfsd 2008/3/27/12b9bf36-d133-4276-ba1d-88f21357ecb2.jpg

现在的查询结果是3行3列的,我想该成1行9列的

谢谢
...全文
100 点赞 收藏 16
写回复
16 条回复
切换为时间正序
当前发帖距今超过3年,不再开放新的回复
发表回复
流年 2008-04-02
if object_id('tempdb..#a') is not null
drop table #a
select identity(int,1,1) as CurIdx,*
into #a
from 你的表

select aa.[Name],aa.Subject,aa.Result,bb.[Name],bb.Subject,bb.Result,cc.[Name],cc.Subject,cc.Result
from
(select 1 as MyIdx,[name],Subject,Result from #a where CurIdx=1) aa
inner join
(select 1 as MyIdx,[name],Subject,Result from #a where CurIdx=2) bb
on aa.MyIdx=bb.MyIdx
inner join
(select 1 as MyIdx,[name],Subject,Result from #a where CurIdx=3) cc
on aa.MyIdx=cc.MyIdx
回复
cson_cson 2008-04-02
select * from 
(select * from tb a where (select count(*) from tb where id >= a.id) = 1 ) a,
(select * from tb a where (select count(*) from tb where id >= a.id) = 2 ) b,
(select * from tb a where (select count(*) from tb where id >= a.id) = 3 ) c


回复
utpcb 2008-04-02
写个动态行转列吧
回复
cson_cson 2008-04-02
select a.*,b.*,c.* from
(select top 1 * from tb order by id) a,
(select top 1 * from tb where id <>
(select top 1 * from tb order by id ) order by id) b,
(select top 1 * from tb order by id desc) c
回复
cson_cson 2008-04-02
select a.*,b.*,c.* from
(select top 1 * from tb order by id) a,
(select * from tb where id not in(select top 1 * from tb order by id )) b,
(select top 1 * from tb order by id desc) c


回复
fengb4676 2008-04-02
这个我觉的在程序里实现比较好,10楼的方式太复杂了,如果再加上参数的话,就更复杂了
回复
sweetweiwei 2008-04-02
select (select top 1 ID from a order by ID) as ID,
(select top 1 Title from a order by ID) as Title,
(select top 1 Icon from a order by ID) as Icon,
(select top 1 ID from (select top 2 * from a order by ID) t order by ID desc) as ID,
(select top 1 Title from (select top 2 * from a order by ID) t order by ID desc) as Title,
(select top 1 Icon from (select top 2 * from a order by ID) t order by ID desc) as Icon,
(select top 1 ID from (select top 3 * from a order by ID) t order by ID desc) as ID,
(select top 1 Title from (select top 3 * from a order by ID) t order by ID desc) as Title,
(select top 1 Icon from (select top 3 * from a order by ID) t order by ID desc) as Icon
回复
cwlcwr 2008-04-02
这个应该是程序里控制
而不是SQL控制吧
回复
把你的表自身与自身做一个笛卡尔集行不行?
select *
from table a,table b
回复
greenberet007 2008-04-02
请麻烦你把sql语句补全可以吗?
谢谢
回复
greenberet007 2008-04-02
是的 谢谢
回复
-狙击手- 2008-04-02
case when id%3

group by id/3
回复
-狙击手- 2008-04-02
ID Title Icon ID Title Icon ID Title Icon

这样?
回复
liangCK 2008-04-02
哦..这样子.
回复
greenberet007 2008-04-02
现在我确定了只要前3条记录
回复
liangCK 2008-04-02
如果有5条记录..就1行15列?
如果有100条记录?.就?.
回复
相关推荐
发帖
MS-SQL Server
创建于2007-09-28

3.3w+

社区成员

MS-SQL Server相关内容讨论专区
申请成为版主
帖子事件
创建了帖子
2008-04-02 04:31
社区公告
暂无公告