简体   繁体   English

MySQL子查询未返回预期结果

[英]MySQL Subquery not returning expected results

I have a MySQL table with communities and their corresponding MLS map area. 我有一个带有社区及其对应的MLS映射区域的MySQL表。 Some communities are large and take up multiple map areas. 一些社区很大,占用多个地图区域。

I am attempting to do a query that returns the most common communities and their map area when passed multiple map areas. 我正在尝试执行一个查询,当通过多个地图区域时,该查询将返回最常见的社区及其地图区域。

The query I am trying for map areas 601 and 606 is: 我正在尝试查询地图区域601和606的查询是:

SELECT DISTINCT(community), mapArea FROM (
    SELECT community, mapArea
    FROM single_family
    GROUP BY community
    ORDER BY COUNT(community) DESC) AS query1
WHERE mapArea LIKE '601%' OR mapArea LIKE '606%' ORDER BY community

Example single_family Table Layout (actual table has over 60k rows): 示例single_family表布局(实际表有6万多行):

community    mapArea
Solera       606 - Henderson
Solera       606 - Henderson
Solera       204 - East
Solera       606 - Henderson
Solera       202 - East
Anthem       606 - Henderson
Green Valley 601 - Henderson
Green Valley 601 - Henderson
Green Valley 606 - Henderson
Seven Hills  606 - Henderson
Seven Hills  606 - Henderson

If I run a count on the table it shows: 如果我对表进行计数,则显示:

community      mapArea           countCommunity
Anthem         606 - Henderson   776
Solera         606 - Henderson   58
Solera         204 - East        6
Solera         202 - East        1
Green Valley   601 - Henderson   188
Green Valley   606 - Henderson   117
Seven Hills    606 - Henderson   372

When I run the above query for map areas 601 and 606 I get the following which is correct for some communites but the community of Solera for example is not listed: 当我对地图区域601和606运行以上查询时,我得到以下内容对某些社区是正确的,但未列出Solera社区:

community     mapArea
Anthem        606 - Henderson
Green Valley  601 - Henderson
Seven Hills   606 - Henderson

Since Solera has the most rows with the map area being 606 - Henderson, I am wondering what is wrong in the query to why it is not being included. 由于Solera的行最多,地图区域为606-Henderson,所以我想知道查询中为什么存在错误,为什么不包含它。

Any help to why this is not returning the expected results and what I have to do to get the expected results is very much appreciated. 非常感谢您对为什么它没有返回预期结果以及我为获得预期结果所做的任何帮助。

You are grouping by community , which in result, might hide mapArea lines within the grouped lines. 您正在按community分组,因此可能会将mapArea线隐藏在分组的线内。 try this instead: 试试这个代替:

SELECT community, mapArea FROM (
    SELECT community, mapArea
    FROM single_family
    WHERE mapArea LIKE '601%' OR mapArea LIKE '606%' 
    GROUP BY community
    ORDER BY COUNT(community) DESC) AS query1
ORDER BY community

also, why using DISTINCT when you're really grouping by the same column ? 另外,当您真正按同一列分组时,为什么还要使用DISTINCT呢? the subquery will result in rows with distinct values of communities. 子查询将导致具有不同社区值的行。 using DISTINCT will just slow down the process 使用DISTINCT只会减慢该过程

Communities are in multiple map areas. 社区位于多个地图区域。 How do you intend to handle that? 您打算如何处理?

Your outer query really isn't needed. 确实不需要您的外部查询。 You can phrase your query as: 您可以将查询短语如下:

SELECT community, mapArea
FROM single_family
WHERE mapArea LIKE '601%' OR mapArea LIKE '606%'
GROUP BY community
ORDER BY community;

If you want all the mapArea s listed as one column, use group_concat() : 如果要将所有mapArea列为一列,请使用group_concat()

SELECT community, group_concat(mapArea) as MapAreas
FROM single_family
WHERE mapArea LIKE '601%' OR mapArea LIKE '606%'
GROUP BY community
ORDER BY community;

If you are content to have them on separate rows, include the column in the group by : 如果您愿意将它们放在单独的行中,请将该列包括在group by

SELECT community, mapArea
FROM single_family
WHERE mapArea LIKE '601%' OR mapArea LIKE '606%'
GROUP BY community, MapArea
ORDER BY community;

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

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