declare @t table(id int)insert into @t select 1 union select 2 union select 3select distinct a.id,b.id,c.id from @t a,@t b,@t c
如果个数的个数不定,只能是写循环或动态语句去CROSS JOIN吧
子陌大哥的好像不怎麼對. 試試這個 declare @t table(id int)insert into @t select 1 union select 2 union select 3select * from @t a left join @t b on a.id<>b.id left join @t c on a.id<>c.id and b.id<>c.id /*the result:*/ id id id ----------- ----------- ----------- 1 2 3 1 3 2 2 1 3 2 3 1 3 1 2 3 2 1
試試這個
declare @t table(id int)insert into @t select 1 union select 2 union select 3select * from @t a left join @t b on a.id<>b.id
left join @t c on a.id<>c.id and b.id<>c.id /*the result:*/
id id id
----------- ----------- -----------
1 2 3
1 3 2
2 1 3
2 3 1
3 1 2
3 2 1