2010-10-23 70 views
0

什么是获取数据了,从下表中的最好的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 
+0

这不是除了存储过程可能的。您也可以使用PHP代码从记录集中获得总和。 – Sadat 2010-10-23 13:17:25

回答