我需要帮助编写一个mysql查询,在一个列中的金额,另一个列值可能是不同的,取决于它包含的值。这是我的表格的一个例子。
account type amount
------- ---- ------
Dell Inventory 500.00
Microsoft Inventory 100.00
Cisco Inventory 300.00
Dell Inventory 400.00
Webhosting Service 15.00这是我到目前为止所做的,虽然不是很多,但我想不出一个好的方法来做这件事。
$result = mysql_query("SELECT * FROM table WHERE type = 'Inventory'");
while($row = mysql_fetch_array($result)){
Sum the amounts for each account type. The account types are not fixed, will change regularly
}正如我上面的注释所说,帐户字段将不会固定,因此我不能用帐户的名称硬编码我的查询。
那么我想要一份报告,详细说明每个账户的总额。
Dell $900
Microsoft $100
Cisco $300谢谢你的帮忙!
发布于 2013-05-21 23:23:32
您需要使用聚合函数SUM()来获取每个账户的总金额。
SELECT account, SUM(amount) totalAmount
FROM tableName
WHERE type = 'inventory'
GROUP BY account发布于 2013-05-21 23:24:33
我认为您可以使用GROUP BY按account对记录进行分组,因此您可以使用SUM()函数将所有金额相加
SELECT account, SUM(amount) as total
FROM table
WHERE type = 'inventory'
GROUP BY account发布于 2013-05-21 23:30:01
SELECT `account`,sum(`amount`) FROM `table_name` WHERE `type`='Inventory' group by `account`https://stackoverflow.com/questions/16673643
复制相似问题