如何在MySQL查询中使用DISTINCT而不在输出中显示该字段?

时间:2013-01-30 13:07:37

标签: mysql

我有这个MySQL查询:

select Region,
CONCAT('$', FORMAT(AVG(sales), 0)) as 'Average_Sales_by_Region', 
count(*) as '# of Dist in state'
from dist, Regions_US
where dist.state=Regions_US.State
group by Region ORDER BY AVG(sales) DESC;

以下是MySQL查询的输出:

+--------------------+-------------------------------+-------------------------+
| Region             | Average_Sales_by_Region       | # of Dist in state      |
+--------------------+-------------------------------+-------------------------+
| Alaska-Hawaii      | $8,150                        |                       4 |
| Mountain           | $20,216                       |                      74 |
| West North Central | $18,268                       |                      40 |
| South Atlantic     | $16,225                       |                     178 |
| East South Central | $14,967                       |                      30 |
| West South Central | $13,704                       |                     125 |
| East North Central | $12,668                       |                      79 |
| New England        | $11,916                       |                      32 |
| Pacific            | $11,553                       |                     120 |
| Middle Atlantic    | $10,292                       |                     131 |
+--------------------+-------------------------------+-------------------------+

有一个名为company_name的字段,我想对其进行DISTINCT:

select DISTINCT company_name,
Region,CONCAT('$', FORMAT(AVG(sales), 0)) as 'Average_Sales_by_Region', 
count(*) as '# of Dist in state'
from dist, Regions_US
where dist.state=Regions_US.State
group by Region ORDER BY AVG(sales) DESC;

我希望它在'company_name'的字段名称上是DISTINCT,但我不希望在输出中显示company_name字段。有没有办法在没有显示的情况下执行DISTINCT company_name?它的语法是什么?它将在上面的MySQL查询中发生什么?或者还有另一种方法吗?谢谢!

1 个答案:

答案 0 :(得分:2)

将其添加到GROUP BY子句并将其从SELECT子句中删除:

SELECT
  Region,
  CONCAT('$', FORMAT(avgSales, 0)) as 'Average_Sales_by_Region', 
  TheCount as '# of Dist in state'
FROM
(
   SELECT 
     Region, 
     AVG(Sales) avgSales, 
     COUNT(*) theCount
   from dist, Regions_US
   where dist.state=Regions_US.State
   group by Region,company_name
) AS sub       
ORDER BY avgSales DESC;

更新:如果您想在同一sales条款中选择AVG(sales)SELECT,则无法执行此操作不应该。 MySQL允许您这样做,但对于每组sales,您将获得Region, company_name的任意值。

使用GROUP BY时,这称为单值规则,这是标准配置。这意味着,SELECT子句中的每一列都必须是GROUP BY或聚合函数。您不能选择不在GROUP BY中也不在聚合函数中的行。在您的情况下,您已经选择了具有汇总功能sales的{​​{1}},为什么还要在同一查询中选择AVG

然而,如果你想这样做,你可以这样做:

sales

假设SELECT Region, CONCAT('$', FORMAT(avgSales, 0)) as 'Average_Sales_by_Region', TheCount as '# of Dist in state', (SELECT sales FROM dist d2 WHERE d2.Region = d1.Region AND d2.company_name = d1.company_name) AS Sales FROM ( SELECT Region, AVG(Sales) avgSales, COUNT(*) theCount from dist INNER JOIN Regions_US ON dist.state = Regions_US.State group by Region,company_name ) AS sub ORDER BY avgSales DESC; 来自Region, company_name表。

请注意:我在您的查询中使用了dist而不是隐式连接语法,它们是相同的。

但我不确定每组INNER JOIN的{​​{1}}值的一致性。