不在子查询中工作的MySQL组
我有这个问题,而且效果很好。我使用MINhome_价格作为起始价格显示,我使用此查询来查询api和添加WHERE子句的位置,因此如果我按价格搜索,MINhome_价格会发生变化不在子查询中工作的MySQL组,mysql,Mysql,我有这个问题,而且效果很好。我使用MINhome_价格作为起始价格显示,我使用此查询来查询api和添加WHERE子句的位置,因此如果我按价格搜索,MINhome_价格会发生变化 SELECT MIN(home_price) as min_home_price, id, name, community, maplocation, locationLabel, logo FROM ourCommunity INNER JOIN readyBuilt O
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行
当我删除组时,我没有收到任何错误,因为每行的价格是相同的。有人对如何完成我想完成的事情有什么建议吗 如果您添加一个价格作为过滤器,返回的最低价格将不是表的实际最低价格,这是正常的 您可以加入社区分组的每个minhome_价格的结果集
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;
不要对大型数据集使用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个表定义和总体目标,即您想要实现什么,而不是从如何开始解决问题开始,那么我可以帮助您进行更简单的查询。在您的第二次查询中,当您没有分组依据时,您只得到一行,因为您正在为整个表选择minhome_price 当您添加GroupBy时,您将获得每个id、每个姓名、每个社区等的minhome_价格 您需要更改子查询以仅获取外部查询当前行的minhome_价格。将子查询视为一个函数,该函数根据与外部查询中的列匹配的某些参数返回值 我还怀疑您的子查询中只需要一个表——一个包含价格的表。其他表可能在那里为您提供位置分组,因此它们属于外部查询。试着让外部查询在没有最小价格的情况下工作,直到最后 例如 选择id、名称等。。。 , 从p中选择minprice 其中p.id=oc.id 作为明普莱斯 来自我们的社区 内部连接等。。。 分组方式
正如其他答案所示,有很多方法可以实现这一点,比如连接而不是子查询。每种方法都有优点和缺点,除非性能是一个问题,否则只需使用最容易理解和维护的方法。您还需要包括数据库模式。当您可以在外部查询上只显示minhome_价格时,您可以使用为什么需要子查询。简单地看,您可能需要使用派生表而不是子查询。但正如ssaltman所说,我们确实需要更多关于您试图实现的目标以及您的db模式的详细信息。@user979331您是否有机会使用一些示例数据、表结构和最终期望的结果编辑您的文章?更好的是,创建一个sql数据库来处理数据。