How do I get all combinations of data from a MySQL table?

后端 未结 3 957
长情又很酷
长情又很酷 2021-01-14 07:05

I have spent the last five hours trying to get every product option combination from a table but I\'m totally stuck now. I have a table with data like (simplified):

3条回答
  •  长情又很酷
    2021-01-14 07:46

    Given your table... I'm assuming you want every possible combo of value and option. That's a cross join (a join without any ON or where clause limiting the results):

     SELECT a.value_id, b.option_id 
         FROM assigned_options a 
         JOIN assigned_options b 
         GROUP BY a.value_id, b.option_id 
    

    The group by filters out the duplicate results.

    Do you have 2 other tables value and option that you want to pull all combinations of?

提交回复
热议问题