如何在mysql中没有group by子句的mysql中计算总计

时间:2017-12-25 16:20:52

标签: php mysql sql database

我希望在不使用total的情况下获得专栏的group by clause,因为我不想concat任何字符串。

我正在寻找这种期望的结果:

|  customer id | project name | Product Name | Quantity | price  | total    |
+--------------+--------------+--------------+----------+--------+----------+
|     9        |  xyz         |   ppn        |   2      |   2    |   4      |
+--------------+--------------+--------------+----------+--------+----------+
|    11        |    pqr       |    xxx       |    2     |   2    |    4     |
+--------------+--------------+--------------+----------+--------+----------+
|              |              |              |          |        |     8    |<=== Grand total  
+---------------------------------------------------------------------------+

这里是SQL小提琴:http://sqlfiddle.com/#!9/30fdd5/2

1 个答案:

答案 0 :(得分:3)

我认为这可以满足您的需求:

positive

要100%清楚总计是结果集中的最后一行,请添加SELECT customer_id, project_name, product_name, quantity, price, quantity * price as total FROM project_expenses_data UNION ALL SELECT NULL, NULL, NULL, NULL, NULL, sum(quantity * price) FROM project_expenses_data; 子句。