1

I am seeking a way in mySQL to select only records that have corresponding records in a linked table. I am likely suffering tunnel vision, or otherwise missing something simple.

I have the following query which currently works besides this requirement above:

SELECT P.ID, P.NAME, P.SEO_NAME, CI.City, R.Region, C.Country
FROM PROPERTIES P 
LEFT JOIN Cities CI ON P.CITY_ID = CI.CityId
LEFT JOIN Regions R ON P.REGION_ID = R.RegionID
LEFT JOIN Countries C ON P.COUNTRY_ID = C.CountryId 
WHERE APPROVED = '1' AND REGION_ID = '5400' 
ORDER BY RAND() LIMIT 1;

This is related to a previous question of mine, here: Select rows Having Count in linked table

While the answer in this linked thread worked for me at the time, I now require the additional information in the query above. Is there any way I can limit it so that only records with records in the linked table PROPERTY_PHOTOS PP (which links ON P.ID = PP.PROPERTY_ID)

Thanks in advance for any input.

Community
  • 1
  • 1
DaveL
  • 339
  • 4
  • 14

6 Answers6

4

Try using INNER JOIN instead of LEFT JOIN. According to the SQL specifications for INNER JOIN:

The INNER JOIN keyword return rows when there is at least one match in both tables.

For the LEFT JOIN, this becomes:

The LEFT JOIN keyword returns all rows from the left table (table_name1), 
even if there are no matches in the right table (table_name2).
Tom Knapen
  • 2,277
  • 16
  • 31
4

An INNER JOIN should do this for you:

INNER JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID

This will only return records where there is a match in both tables.

abatishchev
  • 98,240
  • 88
  • 296
  • 433
Fenton
  • 241,084
  • 71
  • 387
  • 401
3
SELECT P.ID, P.NAME, P.SEO_NAME, CI.City, R.Region, C.Country
FROM PROPERTIES P 
LEFT JOIN Cities CI ON P.CITY_ID = CI.CityId
LEFT JOIN Regions R ON P.REGION_ID = R.RegionID
LEFT JOIN Countries C ON P.COUNTRY_ID = C.CountryId 
**INNER JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID**
WHERE APPROVED = '1' AND REGION_ID = '5400' 
ORDER BY RAND() LIMIT 1;
Sign
  • 1,919
  • 18
  • 33
3

You will need an "INNER JOIN".

feathj
  • 3,019
  • 2
  • 22
  • 22
1

One more JOIN, but not LEFT

SELECT ...
FROM PROPERTIES P
...
INNER JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID

or just

SELECT ...
FROM PROPERTIES P
...
JOIN PROPERTY_PHOTOS PP ON P.ID = PP.PROPERTY_ID

because they are the same.

Community
  • 1
  • 1
abatishchev
  • 98,240
  • 88
  • 296
  • 433
  • What's the point of this? First comment on my answer that it's not correct, and then post the exact same thing? – Tom Knapen Aug 31 '11 at 14:51
  • abatischev was actually the first answer I tried, and it immediately worked. OF COURSE it is that simple. I was just looking at this an entirely different way. Thank you. – DaveL Aug 31 '11 at 14:55
-1

Note: This query doesn't actually include a photos table, but I will assume you are joining it as PP

SELECT P.ID, P.NAME, P.SEO_NAME, CI.City, R.Region, C.Country
FROM PROPERTIES P 
LEFT JOIN Cities CI ON P.CITY_ID = CI.CityId
LEFT JOIN Regions R ON P.REGION_ID = R.RegionID
LEFT JOIN Countries C ON P.COUNTRY_ID = C.CountryId 
WHERE APPROVED = '1' AND REGION_ID = '5400' AND PP.PROPERTY_ID IS NOT NULL
ORDER BY RAND() LIMIT 1;
MattBelanger
  • 5,280
  • 6
  • 37
  • 34