gpt4 book ai didi

mysql - 如何获得过滤数据的总和

转载 作者:行者123 更新时间:2023-11-29 01:58:48 24 4
gpt4 key购买 nike

我是 php、mysql 的新手。

我在我的数据库中使用了四列,companynameissuedwtreceiptwtworkloss

我使用下面的查询来获取 issuedwt、receiptwt 和 workloss 的总数。

$result = mysql_query("SELECT SUM(issuedwt) AS IssuedTotal, SUM(receiptwt) AS ReceiptTotal, SUM(workloss) AS Balance FROM `worksheet`")
or die(mysql_error());

我想使用基于 companyname 的过滤器选项。(即 company1、company2、company3)

如果我过滤 company1,我只需要 company1 的总数。我该怎么办?

最佳答案

您可以使用 WHERE 子句简单地过滤 companyname:

SELECT  
SUM(issuedwt) AS IssuedTotal,
SUM(receiptwt) AS ReceiptTotal,
SUM(workloss) AS Balance
FROM `worksheet`
WHERE compamnyname = 'company1' -- for example

这将为您提供company1IssuedTotalReceiptTotalBalance


但是,如果您想要获取 companyname 的列表和总计,请使用 GROUP BY companyname,但您仍然可以按公司名称进行过滤:

SELECT companyname, 
SUM(issuedwt) AS IssuedTotal,
SUM(receiptwt) AS ReceiptTotal,
SUM(workloss) AS Balance
FROM `worksheet`
GROUP BY companyname

关于mysql - 如何获得过滤数据的总和,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20770063/

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