4

我现在不太了解 SQL 查询,我有一个问题

SELECT PERSON_REF 
FROM   IMAGES 
WHERE  PERSON_REF IN (SELECT _PERSON_ID 
                      FROM   PERSONS 
                      WHERE  REGION_REF = (SELECT _REGION_ID 
                                           FROM   REGIONS 
                                           WHERE  REGION_ABB = "EU")) 
GROUP  BY PERSON_REF 
HAVING Count(PERSON_REF) >= 3 

这个查询给出了我的结果

2  
4  
5  
6  

现在我想在其他查询中使用该结果,如何做到这一点,我需要创建循环还是有其他方法

SELECT PERSON_REF 
FROM   IMAGES 
WHERE  EFFECT_REF = 2 
       AND PERSON_REF IN ( 2, 4, 5, 6 ) 
4

6 回答 6

1
select Person_Ref from Images  where Person_Ref IN ( 
Select _Person_ID from Persons where
Region_Ref in (select _Region_ID from Regions where Region_Abb = "EU"))
and EFFECT_REF = 2 
group by Person_Ref HAVING COUNT(Person_Ref) >= 3
于 2012-10-15T11:56:05.670 回答
1

尝试这个:

select Person_Ref from Images where Effect_Ref = 2 and Person_Ref IN 
( your first query which gives 2 4 5 6)
于 2012-10-15T11:56:10.177 回答
1

JOIN三个表ImagesPersonsRegions不是这些INs 和子查询:

 SELECT i.Person_Ref 
 FROM Images i
 INNER JOIN Persons p ON i.Person_Ref = p.Person_Ref
 INNER JOIN Regions r ON p.Region_Ref = r.Region_Ref
 WHERE r.Region_Abb = "EU"
   AND i.Effect_Ref = 2 
 GROUP BY i.Person_Ref 
 HAVING COUNT(i.Person_Ref) >= 3
于 2012-10-15T11:59:20.490 回答
1

Try this:

SELECT PERSON_REF 
FROM   IMAGES 
WHERE  EFFECT_REF = 2 
       AND PERSON_REF IN (SELECT PERSON_REF 
                          FROM   IMAGES 
                          WHERE  PERSON_REF IN (SELECT _PERSON_ID 
                                                FROM   PERSONS 
                                                WHERE 
                                 REGION_REF = (SELECT _REGION_ID 
                                               FROM   REGIONS 
                                               WHERE 
                                 REGION_ABB = "EU")) 
                          GROUP  BY PERSON_REF 
                          HAVING Count(PERSON_REF) >= 3) 
于 2012-10-15T11:57:48.193 回答
0

只需将 AND 添加到第一个谓词并指定另一个谓词。

从 PERSON_REF IN 的图像中选择 PERSON_REF(从 REGION_REF =(从 REGION_ABB = "EU")的区域中选择 _REGION_ID

于 2012-10-16T18:35:40.557 回答
0

有几种方法可以做到这一点。您可以使用已经插入的 IN 示例,但它仅在您只需要一列时才有效(这是您的情况,我知道,但是如果您遇到需要更多信息的情况,很高兴知道您可以做什么列)

最简单的方法是将您的查询包装到 parentesys 中并给它一个别名,这样您就可以像使用表一样使用它:

(SELECT PERSON_REF 
FROM   IMAGES 
WHERE  PERSON_REF IN (SELECT _PERSON_ID 
                      FROM   PERSONS 
                      WHERE  REGION_REF = (SELECT _REGION_ID 
                                           FROM   REGIONS 
                                           WHERE  REGION_ABB = "EU")) 
GROUP  BY PERSON_REF 
HAVING Count(PERSON_REF) >= 3 ) MY_ALIAS

然后你可以加入你的其他查询。不过,这通常不是一个好方法。

最好的方法是使用公用表表达式 (CTE),因此您可以执行以下操作:

with MY_FIRST_QUERY as(
SELECT PERSON_REF 
    FROM   IMAGES 
    WHERE  PERSON_REF IN (SELECT _PERSON_ID 
                          FROM   PERSONS 
                          WHERE  REGION_REF = (SELECT _REGION_ID 
                                               FROM   REGIONS 
                                               WHERE  REGION_ABB = "EU")) 
    GROUP  BY PERSON_REF 
    HAVING Count(PERSON_REF) >= 3 
)
select MY_FIRST_QUERY.PERSON_REF  

你可以随心所欲地加入它

于 2012-10-15T12:08:44.297 回答