gpt4 book ai didi

php - 在 1 个查询中对 Sum & Count 列求和或使用 PHP?

转载 作者:行者123 更新时间:2023-12-01 00:13:29 24 4
gpt4 key购买 nike

我想我的问题是我试图在同一个查询中求和一个计数,这可以在 php 中实现以避免这种情况吗? Records 列显示记录数,而 Costs 汇总成本,这很好用,但我想对这两个列求和以显示总计?

Select
tblcontacts.ContactFullName As Name,
Count(tblcases.CaseID)As Records,
Sum(tblcases.CaseCommission) As Costs
From
tblcontacts Inner Join
tblcases On tblcontacts.ContactID =
tblcases.ContactAssignedTo
Group By
tblcontacts.ContactFullName

最佳答案

如果不添加另一个查询并对它们执行 UNION,则不能在 MySQL 中执行此操作,例如:

Select
tblcontacts.ContactFullName As Name,
Count(tblcases.CaseID)As Records,
Sum(tblcases.CaseCommission) As Costs
From
tblcontacts Inner Join
tblcases On tblcontacts.ContactID =
tblcases.ContactAssignedTo
Group By
tblcontacts.ContactFullName

UNION ALL

Select
'--GRAND-TOTAL--',
Count(tblcases.CaseID),
Sum(tblcases.CaseCommission)
From
tblcontacts Inner Join
tblcases On tblcontacts.ContactID =
tblcases.ContactAssignedTo

但是,在这种情况下,您可能会通过 PHP 计算总计获得更好的性能,可能同时遍历行以构建表。

关于php - 在 1 个查询中对 Sum & Count 列求和或使用 PHP?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11262014/

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