将一个SQL查询的结果添加到另一个查询

时间:2012-10-15 11:53:32

标签: mysql sql

我现在对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 ) 

6 个答案:

答案 0 :(得分: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

答案 1 :(得分:1)

试试这个:

select Person_Ref from Images where Effect_Ref = 2 and Person_Ref IN 
( your first query which gives 2 4 5 6)

答案 2 :(得分:1)

试试这个:

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) 

答案 3 :(得分:1)

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

 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

答案 4 :(得分:0)

有几种方法可以做到这一点。 您可以使用IN示例,因为它已经是无法使用但只有在您只需要一列时才会起作用(我知道,这是您的情况,但如果您遇到需要更多的情况,最好知道可以做什么oncolumn)

简单的方法是将你的查询包装到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

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

最好的apporach,它使用公用表格式(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  

并且无论你需要什么,你就加入它

答案 5 :(得分:0)

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

SELECT PERSON_REF 从图像 WHERE PERSON_REF IN(SELECT _PERSON_ID                       来自人                       WHERE REGION_REF =(SELECT _REGION_ID                                            来自地区                                            REGERE_ABB =“EU”)) 和     EFFECT_REF = 2 GROUP BY PERSON_REF 有计数(PERSON_REF)> = 3