我正在尝试按名称收集居住在用户指定州周围的州中的所有家庭。如下所示。但是,在执行代码时出现此错误,但不确定如何解决:
错误:
ERROR: syntax error at or near "x"
LINE 13: families WHERE state_name IN x.names and family_name...
代码:
with x as (
SELECT
bordering_states_names as names
FROM
states
WHERE
state_name = 'Ohio'
)
SELECT
family_name,
state_name
FROM
families WHERE state_name IN x.names and family_name IS NOT NULL
LIMIT 20
在这种情况下,bordering_states_names的值如下所示:
{"Ohio", "Indiana", "West Virginia", "Pennsylvania"}
答案 0 :(得分:1)
您可以将其重写为:
with x as (
SELECT bordering_states_names as names
FROM states
WHERE state_name = 'Ohio'
)
SELECT family_name,
state_name
FROM families
WHERE EXISTS (SELECT 1 FROM x WHERE state_name = ANY (names))
AND family_name IS NOT NULL
LIMIT 20
答案 1 :(得分:1)
对于以下查询,表x
应该带有FROM part
,否则x
可能没有意义。
因此,联接这些表(families
和x
)可能是一种选择,如以下情况:
WITH x AS (
SELECT
bordering_states_names as names
FROM
states
WHERE
state_name = 'Ohio'
)
SELECT f.family_name, f.state_name
FROM families f inner join x
ON ( f.state_name = x.names )
AND family_name IS NOT NULL
LIMIT 20;