2017-04-26 92 views
0

查詢的CTE:使用與= ANY

WITH first_names AS (SELECT DISTINCT fname FROM voters) 
SELECT name 
from nicknames 
WHERE groupi = (
    SELECT nn.groupi 
    FROM nicknames AS nn 
    WHERE name = 'john' 
) AND name != 'john' AND upper(name) = ANY(first_names); 

錯誤: column "first_names" does not exist

如何使一個數組從選擇,並在那裏語句中使用它?

回答

0

你在這裏混淆輔助語句指定的列。 first_names是您的SELECT DISTINCT提供的結果的名稱,而fname是其中包含的列的名稱。

不對整個查詢的正確性發表評論,更正的語句將使用正確的列名稱。

WITH first_names AS (SELECT DISTINCT fname 
        FROM voters) 
SELECT name 
FROM nicknames 
WHERE groupi = (
    SELECT nn.groupi 
    FROM nicknames AS nn 
    WHERE name = 'john' 
) AND name != 'john' AND upper(name) = ANY (first_names.fname); 
0

try JOIN instead of ANY

WITH first_names AS (SELECT DISTINCT fname FROM voters) 
SELECT name 
from nicknames 
JOIN first_names on fname = upper(name) 
WHERE groupi = (SELECT nn.groupi FROM nicknames AS nn WHERE name = 'john') AND name != 'john' 
; 
相關問題