Home > database >  SQL generate Data based of the ids of three tables
SQL generate Data based of the ids of three tables

Time:07-05

I have three tables store, gender, age_group each of these tables have ids. I need to generate table data for each one all possible combinations of the three.

ex. store_id = (1,2,3) gender_id = (1,2,3) age_group_id = (1,2,3)

so that i have a table that looks like this:

|store_id|gender_id|age_group_id|
|:------:|:-------:|:----------:|
|  1     |     1   |      1     |
|  1     |     2   |      1     |
|  1     |     3   |      1     |
|  2     |     1   |      3     |
|  2     |     2   |      3     |
|  3     |     1   |      3     |
|  3     |     2   |      3     |


etc. continuing on until each combination is populated, any suggestions on best approach to do this in SQL

CodePudding user response:

Cross join the three tables:

select
  s.Id as store_id,
  g.Id as gender_id,
  a.Id as age_group_id
from store s 
cross join gender g 
cross join age_group a
  •  Tags:  
  • sql
  • Related