Each add-on can be bundled or not, which makes it binary. The way to visualize the combination is to create a word with a bit for each additional, 1 means that the additional list is in the list, 0 means that it is not. For example, Bench + undershelf + overshelf is 110 (or 011 if the binary string is read in reverse order)
Generating each combination of n bits will give each combination of n additions, it will also give each number from 0 to 2^n - 1 .
We can come back from here:
1. generate a list of numbers from 0 to 2^n - 1 ;
2. convert the number to binary to display a combination of additional functions
3. map each bit to an additional
4. Combine the names of additional functions in the beam description.
SELECT CONCAT(b.Name , COALESCE(CONCAT(' + ' , GROUP_CONCAT(x.Name SEPARATOR ' + ')) , '')) Combination FROM (SELECT p.Name, p.id , LPAD(BIN(uN + tN * 10), e.Dim, '0') bitmap FROM Products p CROSS JOIN (SELECT 0 N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) u CROSS JOIN (SELECT 0 N UNION ALL SELECT 1 UNION ALL SELECT 2 UNION ALL SELECT 3 UNION ALL SELECT 4 UNION ALL SELECT 5 UNION ALL SELECT 6 UNION ALL SELECT 7 UNION ALL SELECT 8 UNION ALL SELECT 9) t INNER JOIN (SELECT COUNT(1) Dim , `Parent ID` pID FROM Extra) E ON e.pID = p.ID WHERE uN + tN * 10 < Pow(2, e.Dim) ) B LEFT JOIN (SELECT @rownum := @rownum + 1 ID , `Parent ID` pID , Name FROM Extra , (Select @rownum := 0) r) X ON x.pID = b.ID AND SUBSTRING(b.bitmap, x.ID, 1) = '1' GROUP BY b.Name, b.bitmap
this query will work for up to six additional functions, then it will need another table of numbers (one digit every three additions).
How it works
Subquery E counts the number of additional functions, this is used in C to restrict the elements generated by the tables of digits u and t (unit and tens) to 2 ^ dim.
The number is converted to binary code on BIN(uN + tN * 10) , and then filled with "0" by the number of elements, generating a combination bitmap.
To use the generated bitmap, each add-on needs a fake identifier that will correspond to the position in it, which means subquery X
Two JOIN subqueries are edited by the nth char of the bitmap: if char is 1, then the extension is included, LEFT connected so as not to lose the product without additional functions.