SQL: Help with LEFT OUTER JOIN
-
22-07-2019 - |
Question
It appears my SQL isn't limiting results based on price
.
In my previous post, SQL: Help me optimize my SQL, people indicated that I should use a LEFT OUTER JOIN.
SELECT homes.home_id,
address,
city,
state,
zip,
price,
photo_id,
photo_url_dir
FROM homes
LEFT OUTER JOIN home_photos ON homes.home_id = home_photos.home_id
AND primary_photo_group_id = home_photo_group_id
AND home_photo_type_id =2
AND display_status = true
AND homes.price BETWEEN 500000 AND 1000000
However, it's still displaying homes whose price is < 500000
I don't understand. Why would the SQL above display homes that have a price less than 500000 when I have a WHERE condition to limit exactly that field.
Thanks for your help.
What I want to do
I want to display both homes with and homes without a home_photo based on criteria like PRICE
between X and Y ... or SQFT
> Z. But these criteria needs to be applied to both those homes with and those homes
without a home_photo
Is this correct?
SELECT homes.home_id,
address,
city,
state,
zip,
price,
photo_id,
photo_url_dir
FROM homes
LEFT OUTER JOIN home_photos ON homes.home_id = home_photos.home_id
AND homes.primary_photo_group_id = home_photos.home_photo_group_id
AND home_photos.home_photo_type_id =2
WHERE homes.display_status = true
AND homes.price BETWEEN 500000 AND 1000000
Solution
the last line should be using WHERE not AND
WHERE homes.price BETWEEN 500000 AND 1000000
The end result is the following SQL:
SELECT
homes.home_id,
homes.address,
homes.city,
homes.state,
homes.zip,
homes.price,
home_photos.photo_id,
home_photos. photo_url_dir
FROM
homes
LEFT OUTER JOIN home_photos ON
homes.home_id = home_photos.home_id
AND homes.primary_photo_group_id = home_photos.home_photo_group_id
AND home_photos.home_photo_type_id =2
WHERE
homes.price BETWEEN 500000 AND 1000000
AND homes.display_status = true
EDIT
now your SQFT would go before the WHERE
AND home_photos.home_photo_type_id =2
AND SQFT <=2000
WHERE homes.price BETWEEN 500000 AND 1000000
OTHER TIPS
Use the join conditions after JOIN keyword and all other filter conditions after the WHERE clause
Try this...
SELECT homes.home_id,
address,
city,
state,
zip,
price,
photo_id,
photo_url_dir
FROM
homes
LEFT OUTER JOIN
home_photos ON homes.home_id = home_photos.home_id
AND
primary_photo_group_id = home_photo_group_id
WHERE
home_photo_type_id =2
AND
display_status = true
AND
homes.price BETWEEN 500000 AND 1000000
You have all the conditions in the join
, you should have some of them in a where
clause to limit the query. Something like:
select
homes.home_id,
address,
city,
state,
zip,
price,
photo_id,
photo_url_dir
from
homes
left join
home_photos on homes.home_id = home_photos.home_id
where
primary_photo_group_id = home_photo_group_id and
home_photo_type_id = 2 and
display_status = true and
homes.price BETWEEN 500000 AND 1000000
As I don't know which table each field comes from, I don't know if the above makes sense. Divide the conditions between the join
and the where
as it fits.