-
JOIN with multiple WHERE
Hi all.
I am currently trying to achieve the following
I have the following two tables
TABLE 1 - USERS
Ident | Name
--------------------
34 | Dave
35 | Adam
36 | Lucy
37 | Tom
TABLE 2 - PROFILE_DATA
PDID | Owner | Name | Value
-----------------------------------------------
660 | 34 | AreaType | Rural
661 | 34 | Country | United Kingdom
662 | 35 | AreaType | Rural
663 | 35 | Country | United States
664 | 36 | AreaType | Rural
665 | 36 | Country | United Kingdom
666 | 37 | AreaType | City
667 | 37 | Country | United Kingdom
I basically want to SELECT all the users from the database who are based in a 'Rural' location and live in the 'United Kingdom'.
In this case this would be 'Dave' and 'Lucy'.
I am aware I must create a JOIN between USERS.IDENT and PROFILE_DATA.IDENT and then execute two 'WHERE' clauses.
However I am unsure how to go about doing this.
Any help would be grately appreciated.
-
You can use use self join and IN clause to get required data. Please find following query for same.
Code:
SELECT * from USERS
where Ident IN (
SELECT a.Owner FROM `PROFILE_DATA` a, `PROFILE_DATA` b
WHERE trim(a.Value) = 'Rular' AND trim(b.Value) = 'United Kingdom'
AND a.Owner = b.Owner)
-