显示两个sql列的百分比

时间:2016-10-18 04:29:56

标签: php sql division percentage

这是我一直在使用的一小段代码。

<?php
$rank = 0;
$rank = 0;
$sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp FROM `members` as m 
left join (
select s.memberID, sum(amount) total  from sponsors as s group by s.memberID
) t on t.memberid = m.memberid
left join (
select s.memberID, sum(amount) total  from sponsors as s group by s.memberID
) p on p.memberid = m.memberid Where type= 'business'
ORDER BY t.total DESC, p.total DESC, username ASC");

while($row = mysql_fetch_object($sql)) 
{
    echo "<tr>";
        $rank = $rank + 1;
        echo "<td><center>$rank</center></td>";
        echo "<td>$row->username</td>";
        echo "<td>$row->total</td>";
        echo "<td>$row->target</td>";
        echo "<td>$row->here i want percentage</td>";
    echo "</tr>";
}
?>

我想要做的是在表格的最后一栏显示总/目标的百分比。不知道最好的方法是什么,并且会热衷于听到一些想法。

我确实尝试过这样做,就像回声中的总数/目标一样,但是认为它可能需要先作为变量或其他东西进行评估?无论我尝试过什么都没有工作......

看了这个,但不确定如何在我的场景中实现它 - MSSQL display percentage calculated from two columns

2 个答案:

答案 0 :(得分:0)

这是逻辑 ((演员(“A / B”为浮动)/“C”)* 100.00)AS“D%” 所有值必须是整数,当您将它们分开时,它必须转换为浮点数,以便计算正确。 希望它对你有用。

答案 1 :(得分:0)

试试这个,

 <?php
    $rank = 0;
    $rank = 0;
    $sql = mysql_query("SELECT m.memberID, username,email,target,type, ifnull(t.total,0) as total, ifnull(m.total,0) as totalp, (ifnull(t.total,0)/target)*100 as present FROM `members` as m 
    left join (
    select s.memberID, sum(amount) total  from sponsors as s group by s.memberID
    ) t on t.memberid = m.memberid
    left join (
    select s.memberID, sum(amount) total  from sponsors as s group by s.memberID
    ) p on p.memberid = m.memberid Where type= 'business'
    ORDER BY t.total DESC, p.total DESC, username ASC");

    while($row = mysql_fetch_object($sql)) 
    {
        echo "<tr>";
            $rank = $rank + 1;
            echo "<td><center>$rank</center></td>";
            echo "<td>$row->username</td>";
            echo "<td>$row->total</td>";
            echo "<td>$row->target</td>";
            echo "<td>$row->present</td>";
        echo "</tr>";
    }
    ?>