0

This query works fine:

SELECT country_name FROM countries WHERE country_id IN (23,86,79)

so I can get the list of countries' name based on those country_id. But what I want is to get them with a subquery like:

SELECT country_name  FROM countries WHERE country_id IN (SELECT office_id FROM countries WHERE country_code='FRA')

I can get the first name of the office Id and not all (in this case should be 4). The office_id and country_id are Int. Any helps?

2
  • 1
    Why you dont use this ?? - SELECT country_name FROM countries WHERE country_code='FRA' Commented Jan 23, 2016 at 20:17
  • the query looks fine. show us some data and expected results so we can see what goes wrong Commented Jan 23, 2016 at 20:26

1 Answer 1

2

why do you need such wrong/long turn query. your posted query (below) with subquery

SELECT country_name 
FROM countries 
WHERE country_id IN ( SELECT office_id 
                      FROM countries 
                      WHERE country_code='FRA' )

Can be simpliy

SELECT country_name 
FROM countries 
WHERE country_code='FRA';
Sign up to request clarification or add additional context in comments.

4 Comments

The subquery returns several id (es: 12,22,43,9). With these id I need to get the corresponding country name, The problem if that if I wrote something like "...IN (12,22,43,9)" the whole query works fine and I get four rows but with the Select as subquery I get only one value
You don't need the subquery in first place. just use the query in answer with the required where condition ... should work fine. if not then do select count(*) from countries where country_code='fra' and see what's the count ... that will tell you what's wrong.
Sorry guys, I've been not so clear. the inner query, "SELECT office_id FROM countries WHERE country_code='FRA'", returns a string with comma separated values (es: "12,22,223") and I would use this with IN clause
Ohh!! man ... no that's a comma separated list and you can't use it in IN caluse since it's not properly quoted. You should consider normalizing your table structure rather.

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.