我想得到,从1~9这9个数字中任意取3个的所有的组合, 
例如: 
123 456 789 
147 258 369 
158 234 679 
278 359 146 
... ... 然后把结果插入到表(ID,G1,G2,G3)中,ID是自增字段。 请问Sql怎样写才最简洁、有效? 先谢谢大家!-----------------
这是原贴,原贴楼主(不孝子,奮發圖強!)说是280种,本人有疑问,发个贴来玩玩。create table #(id varchar(1))
insert into # values('1')
insert into # values('2')
insert into # values('3')
insert into # values('4')
insert into # values('5')
insert into # values('6')
insert into # values('7')
insert into # values('8')
insert into # values('9')
goselect '1'+L2.id+L3.id as num1, M1.id+M2.id+M3.id as num2, R1.id+R2.id+R3.id as num3
into #1
from # L2 join # L3 on L2.id<L3.id
    , # M1 join # M2 on M1.id<M2.id join # M3 on M2.id<M3.id
    , # R1 join # R2 on R1.id<R2.id join # R3 on R2.id<R3.id
where L2.id>='2' and M1.id>='2' and M2.id>='2' and R2.id>='2'
        and M1.id<R1.id
        and L2.id not in (M1.id, M2.id, M3.id, R1.id, R2.id, R3.id)
        and L3.id not in (M1.id, M2.id, M3.id, R1.id, R2.id, R3.id)
        and M1.id not in (M2.id, M3.id, R1.id, R2.id, R3.id)
        and M2.id not in (R1.id, R2.id, R3.id)
        and M3.id not in (R1.id, R2.id, R3.id)
        and R1.id not in (R2.id, R3.id)------
这是可爱小熊的结果.我们来分析下小熊的结果
select distinct *
--into #2
from
(
select num1 from #1
union all
select num2 from #1
union all
select num3 from #1
--order by num1
)a
order by num1num1 
---- 
123
124
125
126
127
128
129
134
135
136
137
138
139
145
146
147
148
149
156
157
158
159
167
168
169
178
179
189
234
235
236
237
238
239
245
246
247
248
249
256
257
258
259
267
268
269
278
279
289
345
346
347
348
349
356
357
358
359
367
368
369
378
379
389
456
457
458
459
467
468
469
478
479
489
567
568
569
578
579
589
678
679
689
789(所影响的行数为 84 行)-----------------------
也就是说原贴楼主要重复的值吗?这是我的结果,谁能指出它哪有问题吗?select distinct a.id+b.id+c.id  from # a,# b, # c
where a.id<>b.id and a.id<>c.id and b.id<>c.id     
---- 
123
124
125
126
127
128
129
132
134
135
136
137
138
139
142
143
145
146
147
148
149
152
153
154
156
157
158
159
162
163
164
165
167
168
169
172
173
174
175
176
178
179
182
183
184
185
186
187
189
192
193
194
195
196
197
198
213
214
215
216
217
218
219
231
234
235
236
237
238
239
241
243
245
246
247
248
249
251
253
254
256
257
258
259
261
263
264
265
267
268
269
271
273
274
275
276
278
279
281
283
284
285
286
287
289
291
293
294
295
296
297
298
312
314
315
316
317
318
319
321
324
325
326
327
328
329
341
342
345
346
347
348
349
351
352
354
356
357
358
359
361
362
364
365
367
368
369
371
372
374
375
376
378
379
381
382
384
385
386
387
389
391
392
394
395
396
397
398
412
413
415
416
417
418
419
421
423
425
426
427
428
429
431
432
435
436
437
438
439
451
452
453
456
457
458
459
461
462
463
465
467
468
469
471
472
473
475
476
478
479
481
482
483
485
486
487
489
491
492
493
495
496
497
498
512
513
514
516
517
518
519
521
523
524
526
527
528
529
531
532
534
536
537
538
539
541
542
543
546
547
548
549
561
562
563
564
567
568
569
571
572
573
574
576
578
579
581
582
583
584
586
587
589
591
592
593
594
596
597
598
612
613
614
615
617
618
619
621
623
624
625
627
628
629
631
632
634
635
637
638
639
641
642
643
645
647
648
649
651
652
653
654
657
658
659
671
672
673
674
675
678
679
681
682
683
684
685
687
689
691
692
693
694
695
697
698
712
713
714
715
716
718
719
721
723
724
725
726
728
729
731
732
734
735
736
738
739
741
742
743
745
746
748
749
751
752
753
754
756
758
759
761
762
763
764
765
768
769
781
782
783
784
785
786
789
791
792
793
794
795
796
798
812
813
814
815
816
817
819
821
823
824
825
826
827
829
831
832
834
835
836
837
839
841
842
843
845
846
847
849
851
852
853
854
856
857
859
861
862
863
864
865
867
869
871
872
873
874
875
876
879
891
892
893
894
895
896
897
912
913
914
915
916
917
918
921
923
924
925
926
927
928
931
932
934
935
936
937
938
941
942
943
945
946
947
948
951
952
953
954
956
957
958
961
962
963
964
965
967
968
971
972
973
974
975
976
978
981
982
983
984
985
986
987(所影响的行数为 504 行)大家来拍砖!

解决方案 »

  1.   

    0分?小气啊! 排列组合答案很明显,不过SQL太复杂。简单才是美: [code=SQL]select a.id+b.id+c.id 
    from # a,# b,# c 
    where a.id <b.id 
    and a.id <c.id 
    and b.id <c.id 
    order by 1[ --(84 行受影响) 
    /code] 
    123 
    124 
    125 
    126 
    127 
    128 
    129 
    134 
    135 
    136 
    137 
    138 
    139 
    145 
    146 
    147 
    148 
    149 
    156 
    157 
    158 
    159 
    167 
    168 
    169 
    178 
    179 
    189 
    234 
    235 
    236 
    237 
    238 
    239 
    245 
    246 
    247 
    248 
    249 
    256 
    257 
    258 
    259 
    267 
    268 
    269 
    278 
    279 
    289 
    345 
    346 
    347 
    348 
    349 
    356 
    357 
    358 
    359 
    367 
    368 
    369 
    378 
    379 
    389 
    456 
    457 
    458 
    459 
    467 
    468 
    469 
    478 
    479 
    489 
    567 
    568 
    569 
    578 
    579 
    589 
    678 
    679 
    689 
    789 (84 行受影响) 
      

  2.   


    select a.id+b.id+c.id 
    from # a,# b,# c 
    where a.id <b.id 
    and a.id <c.id 
    and b.id <c.id 
    order by 1--(84 行受影响) 
     
      

  3.   


    晕,在用 SQL 解之前最好还是用别的方法算出答案先
    看清楚:从1~9这9个数字中任意取3个的所有的组合什么是排列?什么是组合?连8开头和9开头的数字都没有? 看来要先学学数学了,赫赫
    排列 P(9,3)=9*8*7=504
    组合 C(9,3)=(9*8*7) / (1*2*3)=84
    实在不会算大数就用小树来套一下:3个数字中任意取3个的所有的组合 , 1 种 =(3*2*1) / (1*2*3)
    4个数字中任意取3个的所有的组合 , 4 种 =(4*3*2) / (1*2*3)
    5个数字中任意取3个的所有的组合 , 10 种 =(5*4*3) / (1*2*3)
    ...
    ...组合式没有顺序的
      

  4.   

    所谓"任意3位数"实际上就是"排列",有顺序的,这更简单select a.id+b.id+c.id 
    from # a,# b,# c "排列"和"组合"是严格区分的,不要说在你的世界里不区分哈
      

  5.   

    条件忘写了where a.id <>b.id and a.id <>c.id and b.id <>c.id    
      

  6.   


    正解。公式P是指排列,从N个元素取R个进行排列(即排序)。
    公式C是指组合,从N个元素取R个,不进行排列(即不排序)。
    组合公式:
    M
    C N=N! / M!(N-M)!排列公式:
    M
    P N=N! / (N-M)!排列和组合的区别关键在顺序问题。相同的排列:元素和顺序都要求一致相同的组合:只要求被取元素相同
      

  7.   


    也可以用C(9,3)*C(6,3)/P(3,3),反正结果就是280,,,
      

  8.   

    楼上的,“123456789分成三组,不同的组合数”这又是另外一个问题了,另当别论。你的答案我倒是觉得除2减半才对吧C(9,3)*C(6,3)/P(3,3)/2 = 140 ?
      

  9.   


    select '1'+L2.id+L3.id as num1, M1.id+M2.id+M3.id as num2, R1.id+R2.id+R3.id as num3 from # L2 join # L3 on L2.id <L3.id 
        ,# M1 join # M2 on M1.id <M2.id join # M3 on M2.id <M3.id 
        ,# R1 join # R2 on R1.id <R2.id join # R3 on R2.id <R3.id 
    where   L2.id>='2' 
        and M1.id>='2' 
        and M1.id <R1.id 
        and L2.id not in (M1.id, M2.id, M3.id, R1.id, R2.id, R3.id) 
        and L3.id not in (M1.id, M2.id, M3.id, R1.id, R2.id, R3.id) 
        and M1.id not in (R1.id, R2.id, R3.id) 
        and M2.id not in (R1.id, R2.id, R3.id) 
        and M3.id not in (R1.id, R2.id, R3.id) 
    order by 1,2