想实现无论数据量多少都只显示4条数据。

520luoxp 2005-12-29 04:37:56
多的时候可以这样:
select top 4 Title,Pic_Name,Url,adv_position from Advertising WHERE Show = 1 order by adv_position
得到:
位置3 2005-12-2913-57-15-banner1.jpg # 3
位置4 2005-12-2913-56-47-banner2.jpg # 4

少的时候怎么办呢。
我想实现少的时候。例如有二条时。自动生成二行并是并用空格填充:
1
2
位置3 2005-12-2913-57-15-banner1.jpg # 3
位置4 2005-12-2913-56-47-banner2.jpg # 4
且保证adv_position列分别只能是1,2,3,4
...全文
283 15 打赏 收藏 转发到动态 举报
写回复
用AI写文章
15 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhang_yzy 2005-12-30
  • 打赏
  • 举报
回复
select *
from
( select top 4
Title,Pic_Name,Url,adv_position
from
(select Title,Pic_Name,Url,adv_position
from Advertising
union all
select '','','',1
union all
select '','','',2
union all
select '','','',3
union all
select '','','',4 ) a
WHERE
Show = 1 ) b
order by
adv_position
-狙击手- 2005-12-29
  • 打赏
  • 举报
回复
declare @t table(title char(10),pic_name char(30),url char(10),adv_position int)
insert into @t
select '位置3','2005-12-2913-57-15-banner1.jpg', '#',3 union all
select '位置4','2005-12-2913-56-47-banner2.jpg','#',4
select * from @t
select top 2000 rowid = identity(int,1,1) into #t from sysobjects a, sysobjects b
select top 4 a.rowid,isnull(b.Title,'') as title ,isnull(b.Pic_Name,'') as pic_name,isnull(b.Url,'') as url from #t a, @t b
where a.rowid *= b.adv_position
select * from #t

drop table #t
/*


rowid title pic_name url
----------- ---------- ------------------------------ ----------
1
2
3 位置3 2005-12-2913-57-15-banner1.jpg #
4 位置4 2005-12-2913-56-47-banner2.jpg #

(所影响的行数为 4 行)
*/
-狙击手- 2005-12-29
  • 打赏
  • 举报
回复
declare @t table(title char(10),pic_name char(30),url char(10),adv_position int)
insert into @t
select '位置3','2005-12-2913-57-15-banner1.jpg', '#',3 union all
select '位置4','2005-12-2913-56-47-banner2.jpg','#',4
select * from @t
select top 2000 rowid = identity(int,1,1) into #t from sysobjects a, sysobjects b
select top 4 a.rowid,isnull(b.Title,''),isnull(b.Pic_Name,''),isnull(b.Url,'') from #t a, @t b
where a.rowid *= b.adv_position
select * from #t

drop table #t
/*


rowid
----------- ---------- ------------------------------ ----------
1
2
3 位置3 2005-12-2913-57-15-banner1.jpg #
4 位置4 2005-12-2913-56-47-banner2.jpg #

(所影响的行数为 4 行)
*/
-狙击手- 2005-12-29
  • 打赏
  • 举报
回复
declare @t table(title char(10),pic_name char(30),url char(10),adv_position int)
insert into @t
select '位置3','2005-12-2913-57-15-banner1.jpg', '#',3 union all
select '位置4','2005-12-2913-56-47-banner2.jpg','#',4
select * from @t
select top 2000 rowid = identity(int,1,1) into #t from sysobjects a, sysobjects b
select top 4 a.rowid,b.Title,b.Pic_Name,b.Url from #t a, @t b
where a.rowid *= b.adv_position
select * from #t

drop table #t
/*

rowid Title Pic_Name Url
----------- ---------- ------------------------------ ----------
1 NULL NULL NULL
2 NULL NULL NULL
3 位置3 2005-12-2913-57-15-banner1.jpg #
4 位置4 2005-12-2913-56-47-banner2.jpg #

(所影响的行数为 4 行)
*/
mislrb 2005-12-29
  • 打赏
  • 举报
回复
select id=identity(int,1,1),Title,Pic_Name,Url,adv_position
into #t
from Advertising
WHERE Show = 1
order by adv_position
javanow 2005-12-29
  • 打赏
  • 举报
回复
如果你能保证你的记录都生成 1,2,3,4这样的序号,我倒有个办法

create table t_seq
(seqid int)
go
insert into t_seq values(1)
insert into t_seq values(2)
insert into t_seq values(3)
insert into t_seq values(4)
go

select a.*,b.seqid from (
select top 4 Title,Pic_Name,Url,adv_position from Advertising WHERE Show = 1 order by adv_position) a right join t_seq b on a.adv_position = b.seqid

大致就是这样,我没有测试,可能语法有点问题。

------------------------------------
http://chinadba.cn
深圳骄子数据库服务网
最具实战经验的数据库优化、培训、设计、管理网站。
下一步更新优化文档栏目



mislrb 2005-12-29
  • 打赏
  • 举报
回复
select id=identity(int,1,1),Title,Pic_Name,Url,adv_position from Advertising WHERE Show = 1 order by adv_position

-狙击手- 2005-12-29
  • 打赏
  • 举报
回复
select top 2000 rowid = identity(int,1,1) into #t from sysobjects a, sysobjects b
select top 4 a.rowid b.Title,b.Pic_Name,b.Url from #t a, table b
where a.rowid *= b.adv_position
select * from #t

drop table #t
520luoxp 2005-12-29
  • 打赏
  • 举报
回复
所以要自动生成
lw1a2 2005-12-29
  • 打赏
  • 举报
回复
有3,4就只填充1,2
有1,3就只填充2,4
反正1,2,3,4都要有
--------------
你那个adv_position怎么得到的?
520luoxp 2005-12-29
  • 打赏
  • 举报
回复
不行
questionanswercsdn 2005-12-29
  • 打赏
  • 举报
回复
用id属性不行吗?

520luoxp 2005-12-29
  • 打赏
  • 举报
回复
有3,4就只填充1,2
有1,3就只填充2,4
反正1,2,3,4都要有
lw1a2 2005-12-29
  • 打赏
  • 举报
回复
空行填充的是1,2?而不是3,4?
520luoxp 2005-12-29
  • 打赏
  • 举报
回复
up

34,873

社区成员

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

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