It appears my SQL isn't limiting results based on price
.
看来我的SQL不是基于价格限制结果。
In my previous post, SQL: Help me optimize my SQL, people indicated that I should use a LEFT OUTER JOIN.
在我之前的帖子中,SQL:帮助我优化我的SQL,人们表示我应该使用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.
我不明白。当我有一个WHERE条件来精确限制该字段时,为什么上面的SQL会显示价格低于500000的房屋。
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
我希望根据X和Y之间的PRICE或SQFT> Z等条件显示家庭和没有home_photo的家庭。但是这些标准需要应用于那些家庭和没有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
4 个解决方案
#1
the last line should be using WHERE not AND
最后一行应该使用WHERE而不是AND
WHERE homes.price BETWEEN 500000 AND 1000000
The end result is the following SQL:
最终结果是以下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
现在你的SQFT将在WHERE之前
AND home_photos.home_photo_type_id =2
AND SQFT <=2000
WHERE homes.price BETWEEN 500000 AND 1000000
#2
Use the join conditions after JOIN keyword and all other filter conditions after the WHERE clause
在JOIN关键字之后使用连接条件,在WHERE子句之后使用所有其他过滤条件
#3
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
#4
You have all the conditions in the join
, you should have some of them in a where
clause to limit the query. Something like:
您拥有连接中的所有条件,您应该在where子句中使用其中一些条件来限制查询。就像是:
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.
由于我不知道每个字段来自哪个表,我不知道上述内容是否有意义。划分连接和适合的位置之间的条件。
#1
the last line should be using WHERE not AND
最后一行应该使用WHERE而不是AND
WHERE homes.price BETWEEN 500000 AND 1000000
The end result is the following SQL:
最终结果是以下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
现在你的SQFT将在WHERE之前
AND home_photos.home_photo_type_id =2
AND SQFT <=2000
WHERE homes.price BETWEEN 500000 AND 1000000
#2
Use the join conditions after JOIN keyword and all other filter conditions after the WHERE clause
在JOIN关键字之后使用连接条件,在WHERE子句之后使用所有其他过滤条件
#3
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
#4
You have all the conditions in the join
, you should have some of them in a where
clause to limit the query. Something like:
您拥有连接中的所有条件,您应该在where子句中使用其中一些条件来限制查询。就像是:
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.
由于我不知道每个字段来自哪个表,我不知道上述内容是否有意义。划分连接和适合的位置之间的条件。