通过子查询的ID列表选择条目?

时间:2019-03-27 05:50:07

标签: sql postgresql

PostgreSQL数据库中,我发出这样的SQL请求:

SQL

SELECT 
    ARRAY_AGG (QUESTION_ID) AS QUESTIONS 
FROM 
    factors_questions_relationship 
WHERE 
    FACTOR_ID IN (SELECT ARRAY_AGG (FACTOR_ID) AS FACTORS 
                  FROM surveys_factors_relationship 
                  WHERE SURVEY_ID = '9bef1274-f1ee-4879-a60e-16e94e88df38');

错误

  

此SQL请求引发错误:
  SQL错误[42883]:错误:运算符不存在:integer = integer []
  没有运算符匹配给定的名称和参数类型。您可能需要添加显式类型转换。

第二个子查询返回的ID列表:{2,10,12,44,52}。我想在主要查询中使用该ID列表。如何正确制作?

1 个答案:

答案 0 :(得分:1)

您可以在下面尝试-

SELECT ARRAY_AGG (QUESTION_ID) AS QUESTIONS FROM factors_questions_relationship 
where FACTOR_ID IN 
(
   SELECT FACTOR_ID AS FACTORS FROM surveys_factors_relationship 
   WHERE SURVEY_ID = '9bef1274-f1ee-4879-a60e-16e94e88df38'
)