目标:我想使用“带有条款”来选择 列表 国家/地区。以下仅适用于选择一个国家/地区。
WITH parameters AS (SELECT 'Sweden' as Country)
SELECT *
FROM Countries_table
WHERE Countries_table.Country = (SELECT Country FROM parameters)
我想做同样的事情,但要使用附带条款中的国家/地区列表。 我尝试过类似的语法(但均无效):
WITH parameters AS (SELECT ('Sweden', 'Norway', 'Finland') as Country)
SELECT *
FROM Countries_table
WHERE Countries_table.Country IN (SELECT Country FROM parameters)
和...
WITH parameters AS (SELECT ['Sweden', 'Norway', 'Finland'] as Country)
SELECT *
FROM Countries_table
WHERE Countries_table.Country IN (SELECT Country FROM parameters)
正确使用什么语法来实现带子句的列表,并将该列表传递给以下select语句?
列表中每个国家的结果应如下所示:
Country | Town | Population size
---------------------------------------
Sweden | Stockholm | 1 500 000
答案 0 :(得分:1)
在Postgres中,您可以这样做:
WITH parameters (country) AS (
values ('Sweden'), ('Norway'), ('Finland')
)
SELECT *
FROM Countries_table
WHERE Countries_table.Country IN (SELECT Country FROM parameters);
如果Redshift不支持该功能,则需要使用UNION
WITH parameters (country) AS (
select 'Sweden' union all
select 'Norway' union all
select 'Finland'
)
SELECT *
FROM Countries_table
WHERE Countries_table.Country IN (SELECT Country FROM parameters);