4

I don't now much about SQL queries, I have a problem

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 

this query gives my result like

2  
4  
5  
6  

Now I want to use that result in an other query, how to do that, Do I need to create loop or is there any other way

SELECT PERSON_REF 
FROM   IMAGES 
WHERE  EFFECT_REF = 2 
       AND PERSON_REF IN ( 2, 4, 5, 6 ) 
2
  • can you not do this in development code or must you use SQL? Have you had a look at functions in mysql? Commented Oct 15, 2012 at 11:54
  • ok, but how to do that in sql Commented Oct 15, 2012 at 11:56

6 Answers 6

1

try this:

select Person_Ref from Images where Effect_Ref = 2 and Person_Ref IN 
( your first query which gives 2 4 5 6)
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

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) 

Comments

1

JOIN the three tables Images, Persons and Regions instead of these INs and subqueries:

 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

Comments

0

there are several ways you can do that. You can use the IN example as was already ansered but it will work only if you need only one column (which is your case, I know, but it is good to know what you can do if you face a situation where you need more that oncolumn)

The simples would be wrap your query into parentesys and give it an alias so you can use it like if it were a table:

(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

then yo ucan join with your other query. This is usually not a good approach though.

The best apporach, its to use Common Table Expressions (CTEs) so you would do something like this:

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  

and the you join it wth whatever you need

Comments

0

Just add AND to the first predicate and specify another one.

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")) AND EFFECT_REF=2 GROUP BY PERSON_REF HAVING Count(PERSON_REF) >= 3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.