gpt4 book ai didi

php - 如何在mysql中没有group by子句的情况下计算mysql中的总计

转载 作者:太空宇宙 更新时间:2023-11-03 11:33:21 25 4
gpt4 key购买 nike

我想在不使用 group by clause 的情况下获取列的总 total,因为我不想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 Fiddle:http://sqlfiddle.com/#!9/30fdd5/2

最佳答案

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

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;

要 100% 清楚总计是结果集中的最后一行,请添加子句 ORDER BY (customer_id IS NULL) ASC

关于php - 如何在mysql中没有group by子句的情况下计算mysql中的总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47970284/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com