# 不重复排列组合的程序

hongyuan20022003 2008-03-14 09:04:53

a/b/c/e/e 五个字母，形成如下排列：
b-c-d-e

c-d-b-a

d-b-c-a

...全文
358 点赞 收藏 26

26 条回复

zjx2388 2008-03-16

hongyuan20022003 2008-03-15

fcuandy 2008-03-15

JiangHongTao 2008-03-15
22L给的是都不能重复的。

``````declare @s varchar(10),@as char(1),@s1 char(1),@i int,@j int
select @s = 'bcdef',@as = 'a'
declare @t table(s varchar(20))
declare @t1 table(s varchar(20))
select @i = 1,@s = left(@s,len(@s)-1)
while @i <=len(@s)
begin
insert @t1 select substring(@s,@i,1)
set @i = @i +1
end
insert @t select * from @t1 where s <> left(@s,1)
set @i = 1
while @i <len(@s)
begin
insert @t select b.s+a.s from @t1 a,@t b where charindex(a.s,b.s)=0 and a.s <> substring(@s,@i+1,1)
delete @t where len(s)=@i
set @i = @i +1
end
update @t set s = s +@as
select * from @t
/*
s
--------------------
edcba
decba
cdeba
edbca
debca
dbeca
cebda
ebcda
cbeda
*/``````

chaorenwopashei 2008-03-15

JiangHongTao 2008-03-15
declare @s varchar(10),@as char(1),@i int
select @s = 'bcdef',@as = 'a'
declare @t table(s varchar(10))
select @i = 1,@s = left(@s,len(@s)-1)
while @i < len(@s)
begin
insert @t select right(@s,len(@s)-@i)+left(@s,@i)+@as
set @i = @i + 1
end
select * from @t
/*
s
----------
cdeba
debca
ebcda
*/

fcuandy 2008-03-15

-狙击手- 2008-03-14
``````declare @t table(col varchar(2))

insert @t select 'b'
insert @t select 'c'
insert @t select 'd'

select a.col + '-'+ b.col+'-'+c.col+'-a'
from @t a,@t b,@t c
where a.col <> b.col and b.col <> c.col  and a.col <> c.col

and a.col <> 'b'
and b.col <> 'c'
and c.col <> 'd'

/*

-----------
c-d-b-a
d-b-c-a

（所影响的行数为 2 行）
*/

``````

hongyuan20022003 2008-03-14

-狙击手- 2008-03-14
``````declare @t table(col varchar(2))
insert @t select 'a'
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'

select a.col + '-'+ b.col+'-'+c.col+'-'+d.col
from @t a,@t b,@t c,@t d
where a.col <> b.col and b.col <> c.col and c.col <> d.col and a.col <> c.col and a.col <> d.col and
b.col <> d.col and d.col = 'a'
and a.col <> 'b'
and b.col <> 'c'
and c.col <> 'd'

/*
b-c-d-e

-----------
c-d-b-a
d-b-c-a

（所影响的行数为 2 行）
*/

``````

hongyuan20022003 2008-03-14

-狙击手- 2008-03-14

SQL形成 如下结果 ：

c-d-b-a
d-b-c-a

hongyuan20022003 2008-03-14
b-d-c-a

b-c-d-e

hongyuan20022003 2008-03-14
c-d-b-a

d-b-c-a

-狙击手- 2008-03-14

---

hongyuan20022003 2008-03-14

-狙击手- 2008-03-14
[Quote=引用 9 楼 liangCK 的回复:]

[/Quote]

liangCK 2008-03-14

--最后一个为a的记录:
``````declare @t table(col varchar(2))
insert @t select 'a'
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'
insert @t select 'e'

select
a.col+'-'+b.col+'-'+c.Col+'-'+d.Col
from
@T a,@T b,@T c,@T d
where
d.Col='a'
and
c.Col<>d.Col
and
b.Col<>d.Col and b.Col<>c.Col
and
a.Col<>b.Col and a.COl<>c.Col and a.COl<>d.Col``````

-狙击手- 2008-03-14
``````declare @t table(col varchar(2))
insert @t select 'a'
insert @t select 'b'
insert @t select 'c'
insert @t select 'd'
insert @t select 'e'

select a.col + '-'+ b.col+'-'+c.col+'-'+d.col
from @t a,@t b,@t c,@t d
where a.col <> b.col and b.col <> c.col and c.col <> d.col and a.col <> c.col and a.col <> d.col and
b.col <> d.col and d.col = 'a'

/*

-----------
b-d-c-a
b-e-c-a
b-c-d-a
b-e-d-a
b-c-e-a
b-d-e-a
c-d-b-a
c-e-b-a
c-b-d-a
c-e-d-a
c-b-e-a
c-d-e-a
d-c-b-a
d-e-b-a
d-b-c-a
d-e-c-a
d-b-e-a
d-c-e-a
e-c-b-a
e-d-b-a
e-b-c-a
e-d-c-a
e-b-d-a
e-c-d-a

（所影响的行数为 24 行）
*/``````

MS-SQL Server

3.3w+

MS-SQL Server相关内容讨论专区

2008-03-14 09:04