我有这个查询,效果很好。我使用MIN(home_price)
显示为起始价格,我使用此查询来添加api和WHERE子句,因此如果我按价格搜索MIN(home_price)
更改。
SELECT MIN(home_price) as min_home_price,
id,
name,
community,
maplocation,
locationLabel,
logo
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
INNER JOIN rb_locations
ON readyBuilt.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
WHERE isDeleted = 0 AND is_upcoming = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name
所以我的解决方案是使用子查询
SELECT id,
name,
community,
maplocation,
locationLabel,
logo,
(SELECT MIN(home_price) as min_home_price
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
INNER JOIN rb_locations
ON readyBuilt.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
WHERE isDeleted = 0
AND is_upcoming = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name) as org_min_home_price
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
INNER JOIN rb_locations
ON readyBuilt.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt
ON community = home_community
WHERE isDeleted = 0 AND is_upcoming = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name
但是当我执行第二个查询时,我收到此错误
子查询返回超过1行
当我删除GROUP BY
时,我发现MIN(home_price)
没有错误,每行都是相同的。有没有人对如何完成我想要完成的事情有任何建议?
答案 0 :(得分:5)
通常情况下,如果您添加price
作为过滤器,则返回的最低价格将不会是该表的实际最小值。
您可以加入由社区
分组的每分钟(home_price)的结果集SELECT min_home_price,
id,
name,
community,
maplocation,
locationLabel,
logo
FROM ourCommunity
INNER JOIN (select min(home_price) min_home_price, home_community from readyBuilt group by home_community) b on b.home_community = community
INNER JOIN readyBuilt a ON community = a.home_community
INNER JOIN rb_locations ON a.home_location = rb_locations.locationId
WHERE id IN ( SELECT DISTINCT id
FROM ourCommunity
INNER JOIN readyBuilt ON community = home_community
WHERE isDeleted = 0)
AND home_status = 1
GROUP BY id,name,community,mapLocation,locationLabel,logo
ORDER BY name;
答案 1 :(得分:0)
不要在MySQL中使用嵌套子查询来处理大型数据集,否则MySQL最终可能会自行创建临时表,并且查询性能会受到影响。
我尝试删除最后一个嵌套查询,如下所示:
SELECT b.min_home_price,
oc.id, oc.name, oc.community, oc.maplocation, oc.locationLabel, oc.logo
FROM ourCommunity oc
INNER JOIN (select min(home_price) min_home_price, home_community from readyBuilt group by home_community) b on b.home_community = oc.community
INNER JOIN readyBuilt rb ON oc.community = rb.home_community
INNER JOIN rb_locations rbl ON rb.home_location = rbl.locationId
INNER JOIN readyBuilt rb1 ON oc.community = rb1.home_community
WHERE rb1.isDeleted = 0
AND oc.home_status = 1
GROUP BY oc.id, oc.name, oc.community, oc.mapLocation, oc.locationLabel, oc.logo
ORDER BY oc.name;
如果你能用简单的英语上传你的3个表格定义和整体目标,即WHAT
你试图实现而不是从HOW
开始你开始解决问题,可能是我可以帮助简化查询。
答案 2 :(得分:0)
在你的第二个查询中,当你没有选择小组时,你只会获得一行,因为你正在为整个表选择min(home_price)。
当您添加" group by"时,您将获得每个ID,每个社区等每个ID的分钟(home_price)。
您需要更改子查询以获取外部查询的当前行的min(home_price)。可以将子查询看作是根据某些参数(恰好与外部查询中的列匹配)返回值的函数。
我还怀疑你在子查询中只需要一个表 - 一个有价格的表。其他表可能会为您提供位置分组,因此它们属于外部查询。 (尝试让外部查询在没有最低价格和最后价格的情况下工作。)
E.g
选择ID,名称等... ,(( 从p中选择min(price) 其中p.id = oc.id )作为minPrice 来自我们的社区oc 内联等...... 分组......
正如其他答案所示,有很多方法可以实现这一点,例如连接而不是子查询。除非性能是一个问题,否则每个都有利弊,只需使用最容易理解和维护的内容。