为什么带有FORMAT的MySQL忽略了ORDER?

时间:2013-01-29 15:55:32

标签: mysql

第一个MySQL查询工作正常,但是,它没有格式化看起来像钱和只有美元。第二个正确地进行格式化,但它忽略了ORDER并且不使用SALDER中的average_sales。使用CONCACT和FORMAT会导致问题吗?谢谢!

首先,ORDER是正确的:

select Region,AVG(sales) 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 Average_Sales_by_Region DESC;

+--------------------+-------------------------------+-------------------------+
| Region             | Average_Sales_by_Region       | # of Dist in state      |
+--------------------+-------------------------------+-------------------------+
| Mountain           |                    20216.2162 |                      74 |
| West North Central |                    18267.5000 |                      40 |
| South Atlantic     |                    16225.2809 |                     178 |
| East South Central |                    14966.6667 |                      30 |
| West South Central |                    13704.3840 |                     125 |
| East North Central |                    12668.3544 |                      79 |
| New England        |                    11915.6250 |                      32 |
| Pacific            |                    11552.8083 |                     120 |
| Middle Atlantic    |                    10291.6031 |                     131 |
| Alaska-Hawaii      |                     8150.0000 |                       4 |
+--------------------+-------------------------------+-------------------------+

但是我被要求更改它,以便Average_Sales_by_Region显示没有美分的美元金额。所以20216.2162的最高数字需要是20,216美元。

所以我的第二个是正确格式化钱,它忽略了ORDER:

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 Average_Sales_by_Region DESC;

+--------------------+-------------------------------+-------------------------+
| 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 |
+--------------------+-------------------------------+-------------------------+

我被困在这里,我不知道为什么ORDER BY Average_Sales_by_Region DESC在第一个工作,但它不在第二个。唯一的区别是使用CONCAT('$',FORMAT(AVG(销售),0))代替AVG(销售)。谢谢!

4 个答案:

答案 0 :(得分:1)

尝试使用此查询

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

这样您可以像第一个查询一样订购,但显示第二个结果

答案 1 :(得分:1)

您仍然可以按未格式化的平均销售额订购。

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;

答案 2 :(得分:0)

它不会忽略您的ORDER BY,因为FORMAT()会将数字转换为字符串。所以你看到的是基于字符串格式的ORDER BY

所以你应该做的是根据非格式化的值进行排序。我建议这样做:

select Region, AVG(sales) as 'Average_Sales_by_Region_NUMERICAL', CONCAT('$', FORMAT(Average_Sales_by_Region_NUMERICAL, 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 Average_Sales_by_Region_NUMERICAL DESC;

那应该有用。并且不会导致AVG(sales)运行两次。

答案 3 :(得分:0)

试试这个

   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 FORMAT(AVG(sales),0) DESC;