MySQL permutation

前端 未结 3 787
攒了一身酷
攒了一身酷 2021-01-03 06:15

I have two tables. One has products and the other has bundles that go with it. I need to figure out the SQL that allows me to find all the combinations in which I can sell

3条回答
  •  刺人心
    刺人心 (楼主)
    2021-01-03 07:16

    I cannot think of any ingenious way of doing this in mysql, but it is very easy in a scripting language. Here in PHP:

     $item) {
            if ($i & pow(2, $j)) {
                $combo[] = $item;
            }
        }
    
        echo implode(' + ', $combo) . "\n";
    }
    

    prints

    Bench
    Bench + undershelf
    Bench + overshelf
    Bench + undershelf + overshelf
    Bench + sheels
    Bench + undershelf + sheels
    Bench + overshelf + sheels
    Bench + undershelf + overshelf + sheels
    

提交回复
热议问题