MySQL或PHP来计算总记录的百分比?

时间:2012-07-05 09:09:58

标签: php mysql

早上好,我有mysql查询,我想计算查询返回的总记录百分比;

Select
  tblcontacts.ContactFullName As Advisor,
  Count(tblcases.CaseID) As Cases
From
  tblcases Inner Join
  tblcontacts On tblcontacts.ContactID =
    tblcases.ContactAssignedTo
Group By
  tblcontacts.ContactFullName with rollup

输出数据Adv 1 100 | Adv 2 300 | Adv 3 600 | 总计1000.我可以在案例之后添加另一列来计算案例/总计* 100,例如,Adv 1输出写入为10%。根据我的研究,我不确定在php或mysql中这样做是否更好?

感谢您的光临。

2 个答案:

答案 0 :(得分:2)

这段代码可以解决问题:

select
    a.advisor,
    a.cases,
    round((a.cases/b.totalCases)*100,2) as PercentCases
from
(
    Select
      tblcontacts.ContactFullName As Advisor,
      Count(tblcases.CaseID) As Cases
    From
      tblcases Inner Join
      tblcontacts On tblcontacts.ContactID =
        tblcases.ContactAssignedTo
    Group By
      tblcontacts.ContactFullName with rollup
) a,
(
    Select
      Count(tblcases.CaseID) As Cases
    From
      tblcases Inner Join
      tblcontacts On tblcontacts.ContactID =
        tblcases.ContactAssignedTo
) b

如果tblCases表没有第一个查询中返回的记录以外的其他记录,那么你可以简单地使用b语句:

(
    Select
      Count(tblcases.CaseID) As Cases
    From
      tblcases 
) b

基于您提供的代码的PHP版本将是这样的:

<?php
mysql_select_db($database_ghl_portal, $ghl_portal); 
$query_Recordset1 = "
    SELECT 
        tblcontacts.ContactFullName, 
        Count(tblcases.CaseID) As Cases 
    FROM 
        tblcases 
            Inner Join 
            tblcontacts 
            On tblcontacts.ContactID = tblcases.ContactAssignedTo 
    GROUP BY 
        tblcontacts.ContactFullName with rollup";
$totalValue=0;
$i=0;
$Recordset1 = mysql_query($query_Recordset1, $ghl_portal) or die(mysql_error()); 
$row_Recordset1 = mysql_fetch_assoc($Recordset1); 
while ($row_Recordset1 = mysql_fetch_assoc($Recordset1))
{
    $totalValue+=$row_Recordset1["Cases"];
    // Do all your other stuff to put the result into an array here such as
    $yourArray[$i][0]=$row_Recordset1["Advisor"];
    $yourArray[$i][1]=$row_Recordset1["Cases"];
    $i++;
}
$totalRows_Recordset1 = mysql_num_rows($Recordset1); 


// And your output code would look like:
for ($j=0; $j<$totalRows_Recordset1; $j++)
{
    echo "Advisor: ".$yourArray[$j][0]."<br>";
    echo "Count: ".$yourArray[$j][1]."<br>";
    echo "Percentage: ".(round(($yourArray[$j][1]/$totalValue),2)*100)."<br><br>";
}
?>

话虽如此,您应该使用php PDO object进行查询。

至于帮助,很高兴能够回馈这个社区,只是我在这里看到的一小部分内容并得到了有用的答案。

答案 1 :(得分:0)

尝试此查询

SELECT tblcontacts.ContactFullName AS Advisor, 
COUNT( tblcases.CaseID ) AS Cases,
(COUNT( tblcases.CaseID )*100)/(SELECT COUNT( A.CaseID ) FROM tblcases AS A) as Percentage
FROM tblcases
INNER JOIN tblcontacts ON tblcontacts.ContactID = tblcases.ContactAssignedTo
GROUP BY tblcontacts.ContactFullName

考虑数据库架构和数据。

CREATE TABLE IF NOT NOT EXISTS tblcases(   CaseID int(11)NOT NULL AUTO_INCREMENT,   ContactAssignedTo int(11)NOT NULL,   主要关键(CaseID) )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 21;

INSERT INTO tblcasesCaseIDContactAssignedTo)VALUES (1,1),(2,1),(3,1),(4,1),(5,2),(6,2),(7,3),(8,3),(9) ,3),(10,3),(11,4),(12,4),(13,5),(14,15),(15,5),(16,6),(17,7) ),(18,7),(19,8),(20,8);

CREATE TABLE IF NOT NOT EXISTS tblcontacts(   ContactID int(11)NOT NULL AUTO_INCREMENT,   ContactFullName varchar(64)NOT NULL,   主要关键(ContactID) )ENGINE = InnoDB DEFAULT CHARSET = latin1 AUTO_INCREMENT = 7;

INSERT INTO tblcontactsContactIDContactFullName)VALUES(1,'amit 1'),(2,'amit 2'),(3,'amit 3') ,(4,'amit 4'),(5,'amit 5'),(6,'amit 6');

结果将是这样的,这对于数据库中的上述数据是正确的。

顾问案例百分比

amit 1 4 20.0000

amit 2 2 10.0000

amit 3 4 20.0000

amit 4 2 10.0000

amit 5 3 15.0000

amit 6 1 5.0000