mysql查询按类别排序最高销售额

时间:2015-02-05 21:33:51

标签: mysql

如何执行此查询时遇到问题。有三个类别,shipcarplane。我想按大多数销售订购类别。

这是SQL小提琴http://sqlfiddle.com/#!2/e3344/1

这是表值

 id  |  name        |  sales  | category  |
   1 |  mike        |    2    |    ship   |
   2 |  john        |    11   |    car    |
   3 |  david       |    13   |    ship   |
   4 |  pablo       |    24   |    car    |
   5 |  greg        |    13   |    car    |
   6 |  nick        |    1    |    ship   |
   7 |  anderson    |    19   |    ship   |
   8 |  matt        |    10   |    plane  |
   9 |  robbie      |    3    |    ship   |
  10 |  victor      |    1    |    ship   |
  11 |  ben         |    11   |    plane  |
  12 |  rick        |    6    |    ship   |
  13 |  christopher |    16   |    car    |
  14 |  steve       |    8    |    ship   |
  15 |  claudio     |    9    |   plane   |

如何按类别添加总销售额并订购DESC?

1 个答案:

答案 0 :(得分:1)

尝试group byorder by声明。

SELECT category, sum(sales)
FROM table
GROUP BY category
ORDER BY sum(sales) DESC

修改 就像一个建议。你的"类别"应该是数据库模型中的额外实体。