-3

示例 SQL 表:

ID | USER | AMOUNT_SALES | TOTAL
--------------------------------
1  | John | 1500         | 2000 
2  | John | 1400         | 3000  
3  | James| 1200         | 4000   
4  | John | 1100         | 5000   
5  | Anie | 1900         | 8000 

所以我想

SELECT * FROM table 

并使用 while 在 HTML 表上显示结果,但我不想重复用户。

我要创建的示例表:

User  | Amount Sales  | Total 
-----------------------------
John  | 4000          | 10000
James | 1200          | 4000
Anie  | 1900          | 8000
4

3 回答 3

5
SELECT user, 
       sum(amount_sales) as sum_sales, 
       sum(total) as sum_totals
FROM table
group by user
于 2013-10-15T10:32:43.200 回答
2

SQL GROUP BY 语法 SELECT column_name, aggregate_function(column_name) FROM table_name WHERE column_name operator value GROUP BY column_name;

http://www.w3schools.com/sql/sql_groupby.asp

例如,选择用户,sum(amount) as sum from table group by user

于 2013-10-15T10:34:02.713 回答
1

按用户汇总总数和分组将满足您的需要:

SELECT user, SUM(amount_sales),SUM(total) FROM table GROUP BY user;
于 2013-10-15T10:33:00.220 回答