0

I know this question has already asked here, but I can't figure this one out from the previous answers.

We've got 2 tables:

members
-----------------------
| id | country_iso_3 |
-----------------------
  1    USA
  2    DZA
  3    FRA
  4    ILI
  5    USA
  6    USA


members_details
-----------------------
| member_id | city    |
-----------------------
  1          AA
  2          BB
  3          CC
  4          DD
  5          EE
  6          FF

Now I want to query members_details and select the cities which are from the same countries, here "AA", "EE" and "FF" should be the results (because the members are from USA)

I know how to compare different cols from different tables, but here we need to check the second table 'member_id' and the first table 'id (country_iso_3)' somehow!

3 Answers 3

1
Select city from members LEFT JOIN members_details 
ON members.id = members_details.memberid Where country_iso_3 = 'USA' 
Sign up to request clarification or add additional context in comments.

Comments

1

Just JOIN the two tables, with a WHERE clause for the country you want to get the city of it like so:

SELECT md.city 
FROM members m
INNER JOIN members_details md ON m.id = md.memberid
WHERE m.country_iso_3 = 'USA'

SQL Fiddle Demo

Comments

0

This should work, and not only for USA, but for any country which is listed more than once:

SELECT d.city
FROM members m,
     members_details d
WHERE d.member_id = m.id
  AND m.country_iso_3 IN (
    SELECT country_iso_3
    FROM members
    GROUP BY 1
    HAVING count(1) > 1
)

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.