简体   繁体   English

MySQL Group不在子查询中工作

[英]MySQL Group By not working in subquery

I have this query and it works great. 我有这个查询,效果很好。 I use the MIN(home_price) to display as a starting price and I use this query for an api and WHERE clauses get added to it, so if I search by price the MIN(home_price) changes. 我使用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

So my solution was to use a subquery 所以我的解决方案是使用子查询

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

But when I execute the second query, I get this error 但是当我执行第二个查询时,我收到此错误

Subquery returns more than 1 row 子查询返回多行

When I remove the GROUP BY I get no errors by the MIN(home_price) is the same for each row. 当我删除GROUP BYMIN(home_price)没有错误,每行都是相同的。 Does anyone have any suggestion on how to accomplish what I am trying to accomplish? 有没有人对如何完成我想要完成的事情有任何建议?

It's normal that if you add a price as filter the minimum price returned won't be the real minimum of the table. 通常情况下,如果您将price作为过滤器添加,则返回的最低价格将不是表格的实际最小值。

You could join on a result set of each min(home_price) groupped by community 您可以加入由社区分组的每个min(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;

Don't use nested subqueries in MySQL for large dataset, otherwise MySQL could end up creating temporary table on its own and query performance will suffer. 不要在MySQL中使用嵌套子查询来处理大型数据集,否则MySQL最终可能会自己创建临时表,并且查询性能将受到影响。

I've tried to remove the last nested query as following: 我试图删除最后一个嵌套查询,如下所示:

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;

If you could please upload your 3 tables definition and the overall objective in plain English, ie WHAT are you trying to achieve rather than starting with HOW you started to solve the problem, may be I could help with simpler query. 如果你能请上传您的3个表定义,并用简单的英语总体目标,即WHAT是你想实现而不是开始HOW你开始解决这个问题,可能是我可以用更简单的查询帮助。

In your second query, you only get one row when you don't have the group by because you are selecting min(home_price) for the entire table. 在第二个查询中,当您没有组时,只会获得一行,因为您为整个表选择了min(home_price)。

When you add "group by", you are getting a min(home_price) for each id, for each name, for each community, etc. 当您添加“分组依据”时,您将获得每个ID,每个名称,每个社区等的min(home_price)。

You need to change the subquery to just get the min(home_price) for the current row of the outer query. 您需要更改子查询以获取外部查询的当前行的min(home_price)。 Think of the subquery as a function to return the value based on certain parameters (which happen to match the columns in your outer query). 可以将子查询视为基于某些参数(恰好与外部查询中的列匹配)返回值的函数。

I also suspect that you only need one table in your sub query- the one with the prices. 我还怀疑你在子查询中只需要一个表 - 一个有价格的表。 The other tables are probably there to give you the location grouping and so they belong in the outer query. 其他表可能会为您提供位置分组,因此它们属于外部查询。 (Try getting the outer query to work without the min price and as that last). (尝试让外部查询在没有最低价格和最后价格的情况下工作)。

Eg 例如

Select id, name, etc ... , ( select min(price) from p where p.id = oc.id ) as minPrice from our community oc inner join etc ... group by ... 选择id,name等...,(从p中选择min(price)p.id = oc.id)作为我们社区oc inner join等的minPrice ... group by ...

As other answers have shown, there are many ways to achieve that, such as joins instead of subqueries. 正如其他答案所示,有很多方法可以实现这一点,例如连接而不是子查询。 There are pros and cons for each, unless performance is an issue, just use what is easiest to understand and maintain. 除非性能是一个问题,否则每个都有利弊,只需使用最容易理解和维护的内容。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM