I'm trying to figure out how to find all possible combinations (using SQL) for the following situation:
- I have 100 ping pong balls in a bowl (id = 1...100)
- Each ball is one of 4 colors (color = red, green, blue, yellow)
I want to pick 5 balls (without replacement) as follows.
- 1 red ball
- 2 green balls
- 3 blue balls
- 2 yellow balls
- 1 ball that is green, blue, or yellow
How can I determine all possible combinations using SQL as efficiently as possible?
Below is the best I could come up with, but I don't want order to matter (combinations) and I want no replacement:
SELECT pick1.id, pick2.id, pick3.id, pick4.id, pick5.id, pick6.id, pick7.id, pick8.id, pick9.id
FROM bowl AS pick1, bowl AS pick2, bowl AS pick3, bowl AS pick4, bowl AS pick5, bowl AS pick6,
bowl AS pick7, bowl AS pick8, bowl AS pick9
WHERE
pick1.color = "red" AND
pick2.color = "green" AND
pick3.color = "green" AND
pick4.color = "blue" AND
pick5.color = "blue" AND
pick6.color = "blue" AND
pick7.color = "yellow" AND
pick8.color = "yellow" AND
(pick9.color = "green" OR
pick9.color = "blue" OR
pick9.color = "yellow")
CROSS JOINto join your tables. What arent you seeing that you would like to? And what does "no replacement" mean?