VAPPM
VAPPM

Reputation: 73

How to find all possible combinations in SQL table?

SELECT DISTINCT `model` from `goods`

Shows me all unique values of model in a table

SELECT DISTINCT `submodel` from `goods`

Shows me all unique values of submodel in a table

How could I request all possible combinations of model-submodel which exist in this table?

Upvotes: 2

Views: 137

Answers (2)

Shahroozevsky
Shahroozevsky

Reputation: 343

These two queries will give you two tables which each one has only a column. So you can just do something like this:

Select *
FROM (SELECT DISTINCT `model` from `goods`) as A, 
     (SELECT DISTINCT `submodel` from `goods`) as B

This will give you all possible combination of model-submodel in this table.
This type of join is also known as CARTESIAN JOIN

Upvotes: 3

Luca Lupidi
Luca Lupidi

Reputation: 164

SELECT Model,submodel from goods group by model,submodel

Upvotes: 0

Related Questions