简体   繁体   English

如何添加总和并显示在php mysql中的表中

[英]how to add sum and show in the table in php mysql

This is My Code.The problem is...i have 2 coulnm in db.leave type and leavecount.currently my code is going to the leave type and check the leave type against emp id and show the sum..like if half day has 2 count agaist emp id then its shows 2..but i want first check the leavetype and then check the leave count and then show the result. 这是我的代码。问题是...我在db.leave类型和leavecount中有2个coulnm。当前,我的代码将进入请假类型,并根据emp id检查请假类型并显示总和。有2个计数agaemp emp id,然后显示2 ..但是我想先检查请假类型,然后检查请假计数,然后显示结果。

<?php
    $hostname="localhost";  
    $username="root";  
    $password="";  
    $db = "usman";  

    $dbh = new PDO("mysql:host=$hostname;dbname=$db", $username, $password);  

    foreach($dbh->query("SELECT COUNT(*) as cnt
        FROM ams 
        where empid= {$_SESSION['sess_user_id']} 
        GROUP BY leavetype 
        HAVING leavetype = 'HalfDay'") as $HalfDay) 
       {  
       echo "<table ><tr ><td style='border: 0px; '>" . $HalfDay['cnt'] . "</td></tr></table>";    
                    echo "<br>"; 
       }
?>

You can check leavetype before empid but HAVING can't place middle of the query. 您可以检查leavetypeempidHAVING不能把查询的中间。

foreach($dbh->query("SELECT COUNT(*) as cnt
    FROM ams 
    sum(leavetype) as leavecount
    where leavetype = 'HalfDay' and 
    where empid= {$_SESSION['sess_user_id']} and
    where leavecount > 2
    GROUP BY leavetype 
    HAVING leavetype = 'HalfDay'") as $HalfDay) 

this code should work. 此代码应该工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM