sql 的查询问题

yuhuidell 2010-05-24 01:16:52
表t1

列1 列2
a 2
b 1
c 3

我希望能根据列2的值来决定最后的显示
a 2
a 2
b 1
c 3
c 3
c 3
有没有什么好的办法。能通过select直接解决的
...全文
81 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
yuhuidell 2010-05-24
  • 打赏
  • 举报
回复
谢谢。学习了。学习了。
黄_瓜 2010-05-24
  • 打赏
  • 举报
回复
--> 测试数据:[tb]
if object_id('[tb]') is not null drop table [tb]
go
create table [tb]([name] varchar(1),[id] int)
insert [tb]
select 'a',2 union all
select 'b',1 union all
select 'c',3

--------------------------------查询开始------------------------------

select b.* from [tb] b , master..spt_values s
where s.number between 1 and b.[id] and type='p'
/*
name id
---- -----------
a 2
a 2
b 1
c 3
c 3
c 3

(6 行受影响)

*/
francis_shalu 2010-05-24
  • 打赏
  • 举报
回复
学习~~~~~~~~~~~~~
水族杰纶 2010-05-24
  • 打赏
  • 举报
回复
if not object_id('tb') is null
drop table tb
Go
Create table tb([列1] nvarchar(1),[列2] int)
Insert tb
select N'a',2 union all
select N'b',1 union all
select N'c',3
Go
Select a.*
from tb a ,master..spt_values b
where b.number between 1 and [列2] and type='P'
/*
列1 列2
---- -----------
a 2
a 2
b 1
c 3
c 3
c 3

(6 個資料列受到影響)
*/
feixianxxx 2010-05-24
  • 打赏
  • 举报
回复
select k.*
from kof k join master..spt_values s
on k.col2>=s.number
where s.type='p' and s.number>0
order by col1
/*
col1 col2
---------- -----------
a 2
a 2
b 1
c 3
c 3
c 3
*/

这样也可以
feixianxxx 2010-05-24
  • 打赏
  • 举报
回复
select k.*
from kof k join master..spt_values s
on k.col2>=s.number
where s.type='p' and s.number between 1 and (select MAX(col2) from kof)
order by col1
/*
col1 col2
---------- -----------
a 2
a 2
b 1
c 3
c 3
c 3
*/
永生天地 2010-05-24
  • 打赏
  • 举报
回复
用sql2005的with 递归
水族杰纶 2010-05-24
  • 打赏
  • 举报
回复
借助master..spt_values

22,206

社区成员

发帖
与我相关
我的任务
社区描述
MS-SQL Server 疑难问题
社区管理员
  • 疑难问题社区
  • 尘觉
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

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