redshift 中的动态 table 名称

dynamic table name in redshift

我有几个名称相似但前缀不同的表:us_cities、ca_cities 和 uk_cities。 这些表中的每一个都只包含一列 (City_name)。我想将所有这些表合并在一起并得到这样的结果:

select 'US' as country, City_name from US_cities
union all
select 'CA' as country, City_name from CA_cities
union all
select 'UK' as country, City_name from UK_cities

将来我会有更多国家/地区的更多城市表,我想要一个动态的 query/view 来识别相关表 (*_cities) 并将它们添加到联合查询中。我如何在 Redshift 中做到这一点?

您可以使用 information_schema.tables table 获取符合您的命名约定的 table 列表。但是,您需要一个外部进程来替换您的视图。

SELECT * FROM information_schema.tables WHERE table_name LIKE '%_cities';

恕我直言,如果您拥有一个 cities table 并使用视图来创建特定于国家/地区的版本会更好。 :)