gpt4 book ai didi

php - 选择数据以及列的总和

转载 作者:行者123 更新时间:2023-11-29 06:20:27 25 4
gpt4 key购买 nike

从下表中获取数据的最佳 SQL 如下

示例表

   id   name        buyer    amt
1 per disney 10
1 per marie 12
1 per clarence 11
2 pella Magnus 5
2 pella Olof 2
2 pella Asa 4
3 chris Lotta 6
3 chris Wayne 3
3 chris Brad 5

数据输出:

Customer Name : Per
disney 10
marie 12
clarence 11
Total 33

Customer Name : pella
Magnus 5
Olof 2
Asa 4
Total 11

Customer Name : Chris
Lotta 6
Wayne 3
Brad 5
Chris 14

所以最后是顶部的名字,以及该卖家的每个买家和每笔交易,最后是总数。

我想出了这个sql查询,但它相当粗糙。

select name, supplier, amt, (select sum(amt) from transact where name = t.name)as total from transact t

最佳答案

使用 Group By 和 Rollup 来获取总和。

select name, supplier, amt
from transact t
group by name, supplier with rollup;

http://dev.mysql.com/doc/refman/5.1/en/group-by-modifiers.html

关于php - 选择数据以及列的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4004104/

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