比如
a/b/c/e/e 五个字母,形成如下排列:
b-c-d-e
想要得到如下结果:
c-d-b-a
或者
d-b-c-a
总之,相应的位置不能有重复的字母。如果有abcdef六个字母,形成 bcdef这样的排列,如何得出符合的排列?
a/b/c/e/e 五个字母,形成如下排列:
b-c-d-e
想要得到如下结果:
c-d-b-a
或者
d-b-c-a
总之,相应的位置不能有重复的字母。如果有abcdef六个字母,形成 bcdef这样的排列,如何得出符合的排列?
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/*
-----------
a-d-c-b
a-e-c-b
a-c-d-b
a-e-d-b
a-c-e-b
a-d-e-b
a-d-b-c
a-e-b-c
a-b-d-c
a-e-d-c
a-b-e-c
a-d-e-c
a-c-b-d
a-e-b-d
a-b-c-d
a-e-c-d
a-b-e-d
a-c-e-d
a-c-b-e
a-d-b-e
a-b-c-e
a-d-c-e
a-b-d-e
a-c-d-e
b-d-c-a
b-e-c-a
b-c-d-a
b-e-d-a
b-c-e-a
b-d-e-a
b-d-a-c
b-e-a-c
b-a-d-c
b-e-d-c
b-a-e-c
b-d-e-c
b-c-a-d
b-e-a-d
b-a-c-d
b-e-c-d
b-a-e-d
b-c-e-d
b-c-a-e
b-d-a-e
b-a-c-e
b-d-c-e
b-a-d-e
b-c-d-e
c-d-b-a
c-e-b-a
c-b-d-a
c-e-d-a
c-b-e-a
c-d-e-a
c-d-a-b
c-e-a-b
c-a-d-b
c-e-d-b
c-a-e-b
c-d-e-b
c-b-a-d
c-e-a-d
c-a-b-d
c-e-b-d
c-a-e-d
c-b-e-d
c-b-a-e
c-d-a-e
c-a-b-e
c-d-b-e
c-a-d-e
c-b-d-e
d-c-b-a
d-e-b-a
d-b-c-a
d-e-c-a
d-b-e-a
d-c-e-a
d-c-a-b
d-e-a-b
d-a-c-b
d-e-c-b
d-a-e-b
d-c-e-b
d-b-a-c
d-e-a-c
d-a-b-c
d-e-b-c
d-a-e-c
d-b-e-c
d-b-a-e
d-c-a-e
d-a-b-e
d-c-b-e
d-a-c-e
d-b-c-e
e-c-b-a
e-d-b-a
e-b-c-a
e-d-c-a
e-b-d-a
e-c-d-a
e-c-a-b
e-d-a-b
e-a-c-b
e-d-c-b
e-a-d-b
e-c-d-b
e-b-a-c
e-d-a-c
e-a-b-c
e-d-b-c
e-a-d-c
e-b-d-c
e-b-a-d
e-c-a-d
e-a-b-d
e-c-b-d
e-a-c-d
e-b-c-d
(所影响的行数为 120 行)
*/
想要得到如下结果:
c-d-b-a
或者
d-b-c-a
总之,相应的位置不能有重复的字母。 b-c-d-e没有a,要得到的结果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
len(replace(replace(replace(replace('abcde',a.col,''),b.col,''),c.Col,''),d.Col,''))=1
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 行)
*/
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
相应的位置不能有重复的字母。 和b-c-d-e 相比。求bcdef有多少种排列?
或者
d-b-c-a 没有别的
和
b-c-d-e
第一个字母重合,所以排除!
已知条件:b-c-d-e SQL形成 如下结果 :
c-d-b-a
d-b-c-a
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 行)
*/
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 行)
*/
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
*/
下面给出的是不与给定字符串重复,但记录之间还是可以重复。
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
*/