MySQL查询结果的总和

时间:2014-07-08 08:07:22

标签: php mysql sql sum

我在表格中有一个名为“trail”的表我有3列名为“id”,“name”和“grading”。

我有以下查询,它根据id给出了特定路径名称的最后5行。这很有效,但现在我需要在评分栏中得到5个结果的总和吗?

SELECT * FROM `trails` WHERE `name` = "Free Flow" ORDER BY `id` DESC LIMIT 5

2 个答案:

答案 0 :(得分:2)

SELECT sum(`grading`)
FROM
  (SELECT *
   FROM `trails`
   WHERE `name` = "Free Flow"
   ORDER BY `id` DESC LIMIT 5) AS TEMP;

答案 1 :(得分:-1)

您可以使用此查询来获取特定列的总和

SELECT SUM(originalOfferSize)
FROM `ActiveListings`
WHERE `originalOfferSize` = 4000000
ORDER BY `listingId` DESC LIMIT 5