1

第一个 MySQL 查询工作正常,但是,它的格式不像钱,而且只有美元。第二个格式正确,但它忽略了 ORDER 并使 average_sales 不在 ORDER 中。使用 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 美元。

所以我的第二个虽然正确格式化了钱,但它忽略了订单:

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(sales), 0)) 代替 AVG(sales)。谢谢!

4

4 回答 4

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

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

于 2013-01-29T16:00:55.253 回答
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;
于 2013-01-29T16:01:27.490 回答
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)运行两次。

于 2013-01-29T16:02:22.017 回答
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;
于 2013-01-29T16:05:11.500 回答