創建

2017-05-09 44 views
1

的所有可能組合我希望在PostgreSQL中創建來自3個不同表的值的所有可能組合,並將它們組合爲以下劃線_分隔的唯一字符串。創建

例:

table car_type 
column 'type' contains: diesel, gasoline, electric 

table car_color 
column 'color' contains: black, blue, red 

table car_stereo 
column 'checked' contains: true, false 

我想其中包含的所有值的觀點:

diesel_black_true 
diesel_blue_true 
diesel_red_true 
diesel_black_false 
diesel_blue_false 
diesel_red_false 
gasoline_black_true 
gasoline_red_true 
... 

希望這是有道理的,是有可能在通用的和動態的方式?

回答

3

可以Cross Join表讓所有的組合

Select t.type||'_'||c.color||'_'||s.checked 
From car_type t 
Cross join car_color c 
Cross join car_stereo s;