简体   繁体   English

如何在MySQL中没有总计子句的情况下在MySQL中计算总计

[英]How to calculate grand total in mysql without group by clause in mysql

i want to get grand total of a column without using group by clause as i don't want to concat any string. 我想盛大的total列,而无需使用group by clause ,因为我不想concat任何字符串。

i looking for this kind desired result: 我在寻找这种理想的结果:

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

here is SQL Fiddle: http://sqlfiddle.com/#!9/30fdd5/2 这是SQL Fiddle: http ://sqlfiddle.com/#!9/30fdd5/2

I think this does what you want: 我认为这可以满足您的需求:

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;

To be 100% clear that the total is the last row in the result set, add the clause ORDER BY (customer_id IS NULL) ASC . 要100%清除总计是结果集中的最后一行,请添加子句ORDER BY (customer_id IS NULL) ASC

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM