简体   繁体   English

如果在PHP中具有相同的ID和相同的值或值为null,则合并行

[英]Merge rows if have the same id and same value or value is null in PHP

I'm using PHP programming and Microsoft Access database. 我正在使用PHP编程和Microsoft Access数据库。 Both connected using PDO. 两者都使用PDO连接。

I have problem merging column's values in the same table if the Employee ID's "1stHalf" or "2ndHalf" have value on the same month and year. 如果雇员ID的“ 1stHalf”或“ 2ndHalf”在同一月份和年份具有值,则在合并同一表中的列值时出现问题。

This is because in my situation, employee might pay once a month or twice a month. 这是因为在我的情况下,员工可能每月支付一次或每月支付两次。 That is why I have 1st half and 2nd half column in my table. 这就是为什么我的表中有第一半和第二半列的原因。

But the trick is I only have 1 column called "EPFee" and it will be merge based on "1stHalf" or "2ndHalf" value whether that month is once a month or twice a month. 但是诀窍是我只有1个列称为“ EPFee”,并且无论该月是每月一次还是每月两次,都将基于“ 1stHalf”或“ 2ndHalf”值进行合并。

Table that I have now : 我现在有的表:

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPFee    
1011        |  0      |   1     |  2    | 2017 | 15.00   
1011        |  1      |   0     |  2    | 2017 | 29.00

Output that I want : 我想要的输出:

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPF1stFee | EPF2ndFee   
1011        |  1      |   1     |   2   | 2017 |   29.00   |   15.00

If 2ndHalf value is 0 on Month 2/2017 The output should be like this: 如果在2/2017月份2ndHalf的值为0,则输出应如下所示:

Employee ID | 1stHalf | 2ndHalf | Month | Year | EPF1stFee | EPF2ndFee   
1011        |  1      |   0     |   2   | 2017 |   29.00   |   0.00

Here my code updated thanks to sr hs : 在这里,我的代码由于sr hs而更新:

<?php
    $db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\Users\User\Desktop\Demo2018.mdb; Uid=; Pwd=COMPLETEPAYROLL;");

    $sql  = "SELECT `Employee ID`, `1stHalf`, `2ndHalf`, `Month`, `Year` FROM `tblPAyTrans` WHERE `Employee ID` = '1011' AND Month = '2' AND Year='2017'";

    $result = $db->query($sql);

    echo "<br>RESULT: <br><br>";
    echo "<table border='2'>
            <tr>
                <th>Employee ID</th>
                <th>1st Half</th>
                <th>2nd Half</th>
                <th>Month</th>
                <th>Year</th>
                <th>EPF1stFee</th>
                <th>EPF2ndFee</th>
            </tr>";

        $prevEmpId  = "0";
        $FirstHalf  = "0";
        $SecondHalf = "0";
        $Month      = "";
        $Year       = "";
        while ($row = $result->fetch()) {
            // You can take the following three lines out of the loop, as they remain constant
            $EmployeeID = $row['Employee ID'];
            $Month = $row['Month'];
            $Year  = $row['Year'];

            // Only change the value if non-zero
            if ($FirstHalf === "0"){
                $FirstHalf = $row['1stHalf'];
                $query1 = "SELECT `EPFee` FROM `tblPAyTrans` WHERE `Employee ID` = '$EmployeeID' AND `1stHalf` = '$FirstHalf'";
                $result2 = $db->query($query1);
                $getVal1 = $result2->fetch();
                $EPF1stFee = $getVal['EPFee'];
            }

            // Only change the value if non-zero
            if ($SecondHalf === "0"){
                $SecondHalf = $row['2ndHalf'];
                $query2 = "SELECT `EPFee` FROM `tblPAyTrans` WHERE `Employee ID` = '$EmployeeID' AND `2ndHalf` = '$SecondHalf'";
                $result3 = $db->query($query2);
                $getVal2 = $result3->fetch();
                $EPF2ndFee = $getVal2['EPFee'];
            }

        }

        // Print the record
            echo "<tr>";
                echo "<td>" . $EmployeeID. "</td>";
                echo "<td>" . $FirstHalf. "</td>";
                echo "<td>" . $SecondHalf. "</td>";
                echo "<td>" . $Month. "</td>";
                echo "<td>" . $Year. "</td>";
                echo "<td>" . $EP1stFee. "</td>";
                echo "<td>" . $EP2ndFee. "</td>";
            echo "</tr>";

        echo "</table>";
    ?>

Any ideas to solve this?? 有什么解决办法吗?

Does this SQL solve the problem? 此SQL是否可以解决问题?

SELECT `Employee ID`, sum(`1stHalf`) AS 1stHalf, sum(`2ndHalf`) AS 2ndHalf, `Month`, `Year`
FROM tblPAyTrans
GROUP BY `Employee ID`, `Month`, `Year`

Result: 结果:

Employee ID 1stHalf 2ndHalf Month   Year
1011           1       1      2     2017

Check your result here: http://sqlfiddle.com/#!9/98ddd/4 在此处检查结果: http : //sqlfiddle.com/#!9/98ddd/4

If you are looking for solution in PHP, 如果您正在寻找PHP的解决方案,

<?php
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\Users\User\Desktop\Demo2018.mdb; Uid=; Pwd=COMPLETEPAYROLL;");

$sql  = "SELECT `Employee ID`, `1stHalf`, `2ndHalf`, `Month`, `Year` FROM `tblPAyTrans` WHERE `Employee ID` = '1011' AND Month = '2' AND Year='2017'";

$result = $db->query($sql);

echo "<br>RESULT: <br><br>";
echo "<table border='2'>
        <tr>
            <th>Employee ID</th>
            <th>1st Half</th>
            <th>2nd Half</th>
            <th>Month</th>
            <th>Year</th>
        </tr>";

    $prevEmpId  = "0";
    $FirstHalf  = "0";
    $SecondHalf = "0";
    $Month      = "";
    $Year       = "";
    while ($row = $result->fetch()) {
        // You can take the following three lines out of the loop, as they remain constant
        $EmployeeID = $row['Employee ID'];
        $Month = $row['Month'];
        $Year  = $row['Year'];

        // Only change the value if non-zero
        if ($FirstHalf === "0")
            $FirstHalf = $row['1stHalf'];

        // Only change the value if non-zero
        if ($SecondHalf === "0")
            $SecondHalf = $row['2ndHalf'];

    }

    // Print the record
        echo "<tr>";
            echo "<td>" . $EmployeeID. "</td>";
            echo "<td>" . $FirststHalf. "</td>";
            echo "<td>" . $SecondHalf. "</td>";
            echo "<td>" . $Month. "</td>";
            echo "<td>" . $Year. "</td>";
        echo "</tr>";

    echo "</table>";
?>

this can also be done for all employees by the following code, 也可以通过以下代码为所有员工完成此操作,

<?php
$db = new PDO("odbc:DRIVER={Microsoft Access Driver (*.mdb)}; DBQ=C:\Users\User\Desktop\Demo2018.mdb; Uid=; Pwd=COMPLETEPAYROLL;");

$sql  = "SELECT `Employee ID`, `1stHalf`, `2ndHalf`, `Month`, `Year` FROM `tblPAyTrans` order by `EmployeeID` asc";

$result = $db->query($sql);

echo "<br>RESULT: <br><br>";
echo "<table border='2'>
        <tr>
            <th>Employee ID</th>
            <th>1st Half</th>
            <th>2nd Half</th>
            <th>Month</th>
            <th>Year</th>
        </tr>";

    $prevEmpId  = "0";
    $FirstHalf  = "0";
    $SecondHalf = "0";
    $Month      = "";
    $Year       = "";
    while ($row = $result->fetch()) {
        $EmployeeID = $row['Employee ID'];

        // Check if employee id is repeating, if yes, store the required values
        if (($prevEmpId === "0") || ($prevEmpId === $EmployeeID)) {
            if ($prevEmpId === "0")
                $prevEmpId = $EmployeeID;
            if (($FirstHalf === "0") || ($row['1stHalf'] !== "0"))
                $FirstHalf = $row['1stHalf'];
            if (($SecondHalf === "0") || ($row['2ndHalf'] !== "0"))
                $SecondHalf = $row['2ndHalf'];
            $Month = $row['Month'];
            $Year  = $row['Year'];
        }

        // When the employee Id changes print the values onto the table
        else {
            // Print old values
            echo "<tr>";
                echo "<td>" . $EmployeeID. "</td>";
                echo "<td>" . $FirststHalf. "</td>";
                echo "<td>" . $SecondHalf. "</td>";
                echo "<td>" . $Month. "</td>";
                echo "<td>" . $Year. "</td>";
            echo "</tr>";                

            // Set the new values as the employee id  changed
            $prevEmpId  = $EmployeeID;
            $FirstHalf  = $row['1stHalf'];
            $SecondHalf = $row['2ndHalf'];
            $Month      = $row['Month'];
            $Year       = $row['Year'];
        }
    }

    // Print last record
    // As the last record will be skipped before being displayed
        echo "<tr>";
            echo "<td>" . $EmployeeID. "</td>";
            echo "<td>" . $FirststHalf. "</td>";
            echo "<td>" . $SecondHalf. "</td>";
            echo "<td>" . $Month. "</td>";
            echo "<td>" . $Year. "</td>";
        echo "</tr>";

    echo "</table>";
?>

Make sure the query sorted by EmployeeId ASC 确保查询按EmployeeId ASC排序

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

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