1

早上好,我有 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 | 进阶 2 300 | 进阶 3 600 | 总计 1000。我能否在案例之后添加另一列来计算案例/总计 * 100,例如,Adv 1 输出写入为 10%。根据我的研究,我不确定在 php 或 mysql 中执行此操作是否更好?

感谢您的关注。

4

2 回答 2

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 对象进行查询。

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

于 2012-07-05T09:15:15.603 回答
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

考虑数据库架构和数据。

如果不存在则创建表tblcases( CaseIDint(11) NOT NULL AUTO_INCREMENT, ContactAssignedToint(11) NOT NULL, PRIMARY KEY ( CaseID)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=21;

插入tblcases( CaseID, ContactAssignedTo) 值 (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, 5),(15, 5),(16, 6 ),(17, 7),(18, 7),(19, 8),(20, 8);

如果不存在则创建表tblcontacts( ContactIDint(11) NOT NULL AUTO_INCREMENT, ContactFullNamevarchar(64) NOT NULL, PRIMARY KEY ( ContactID)) ENGINE=InnoDB DEFAULT CHARSET=latin1 AUTO_INCREMENT=7;

插入tblcontacts( ContactID, ContactFullName) 值 (1, 'amit 1'),(2, 'amit 2'),(3, 'amit 3'),(4, 'amit 4'),(5, 'amit 5') ,(6, 'amit 6');

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

顾问案例百分比

阿米特 1 4 20.0000

阿米特 2 2 10.0000

阿米特 3 4 20.0000

阿米特 4 2 10.0000

阿米特 5 3 15.0000

阿米特 6 1 5.0000

于 2012-07-05T10:49:00.767 回答