创建所有可能的组合

3

我希望在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
...

希望这有意义,能否以一种通用和动态的方式实现呢?
1个回答

3
您可以使用“Cross Join”连接表格以获取所有组合。
Select t.type||'_'||c.color||'_'||s.checked
From car_type t
Cross join car_color c
Cross join car_stereo s;

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接