以前数据库设计不合理,现在要把
ID Order
1 01
2 0101
3 0102
4 010101
5 01010101
现在要查出类似ID为5的所有上级ID(上级ID就像2是4 和5的上级ID)
print @values
结果要变成这样1,2,4说明:ID5的order 长度是不定的,但最长为18位。
希望高手帮助。。
ID Order
1 01
2 0101
3 0102
4 010101
5 01010101
现在要查出类似ID为5的所有上级ID(上级ID就像2是4 和5的上级ID)
print @values
结果要变成这样1,2,4说明:ID5的order 长度是不定的,但最长为18位。
希望高手帮助。。
解决方案 »
- 初学powerdesigner,生成数据库遇到问题
- 存储过程,变量做表名,返回值的问题.
- SQL能否像ASP那样调用FileSystemObject
- 100分求 SQL2005不用循环行转列,列转行问题
- 每条记录中有个字段叫截至日期,判断如果到达这个截止日期,就将这条记录删除
- sql语句问题,能实现吗?
- 在网址上输入"char(124)+user+char(124)=0"是干什么用的?
- 高手请进。。。开历史先河
- 初来菜鸟急求助 关于图象插入SQL server
- ★★※SQL Server中开始一个事务后,为什么其它的人就不能访问数据库了呢?
- SQL server 2000 这个JOB怎么写
- **********************请教一个关于查询的问题**************************
insert into @t select 1,'01'
insert into @t select 2,'0101'
insert into @t select 3,'0102'
insert into @t select 4,'010101'
insert into @t select 5,'01010101'
declare @values varchar(100)select @values=isnull(@values,'')+','+rtrim(a.ID) from @t a,@t b where b.[Order] like a.[Order]+'%' and a.ID!=b.ID and b.ID=5set @values=stuff(@values,1,1,'')print @values
(
ID int,
[Order] varchar(10)
)
INSERT INTO tbl
SELECT 1,'01'UNION ALL
SELECT 2,'0101'UNION ALL
SELECT 3,'0102'UNION ALL
SELECT 4,'010101'UNION ALL
SELECT 5,'01010101'
SELECT * FROM tbl WHERE charindex(','+[Order],','+'01010101')>0 AND LEN([Order])<LEN('01010101')
--
ID Order
----------- ----------
1 01
2 0101
4 010101(3 行受影响)
insert into @t select 1,'01'
insert into @t select 2,'0101'
insert into @t select 3,'0102'
insert into @t select 4,'010101'
insert into @t select 5,'01010101'declare @id varchar(200)
set @id=''
select @id=@id+','+ltrim(id) from
(
select id from @t where charindex(','+[Order],','+'01010101')>0 and ID!=5
) tb
print stuff(@id,1,1,'')--
1,2,4
insert into table1 select 1,'01'
insert into table1 select 2,'0101'
insert into table1 select 3,'0102'
insert into table1 select 4,'010101'
insert into table1 select 5,'01010101'select * from table1
where charindex([Order],(select [Order] from table1 where id = 5))=1--这里为了避免一些问题最好是=1
where charindex([Order],(select [Order] from table1 where id = 5))=1
and [Order]<>(select [Order] from table1 where id = 5)