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

Posted

技术标签:

【中文标题】如果具有相同的 id 和相同的值或值在 PHP 中为空,则合并行【英文标题】:Merge rows if have the same id and same value or value is null in PHP 【发布时间】:2018-05-17 02:49:41 【问题描述】:

我正在使用 php 编程和 Microsoft Access 数据库。两者都使用 PDO 连接。

如果员工 ID 的“1stHalf”或“2ndHalf”在同一月份和同一年具有值,我在合并同一个表中的列值时遇到问题。

这是因为在我的情况下,员工可能每月支付一次或两次。这就是为什么我的表中有第一半和第二半列的原因。

但诀窍是我只有一个名为“EPFee”的列,无论该月是每月一次还是每月两次,它将根据“1stHalf”或“2ndHalf”值进行合并。

我现在拥有的桌子:

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

我想要的输出:

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

如果 2ndHalf 值在 2017 年 2 月为 0 输出应该是这样的:

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

感谢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>";
    ?>

有什么办法解决这个问题吗??

【问题讨论】:

我已经回滚了您在问题本身中包含的解决方案。请在修订历史中找到它并将其作为自己的答案发布,谢谢。 【参考方案1】:

如果您正在寻找 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>";
?>

这也可以通过以下代码为所有员工完成,

<?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>";
?>

确保查询按 EmployeeId ASC 排序

【讨论】:

第一个选项可以运行,但第一个半值结果是 0 而不是 1。第二个选项根本不起作用。错误提示“在布尔值上调用成员函数 fetch()” 第一个选项有效!,现在我有一个新问题。请检查我上面的更新。【参考方案2】:

这个SQL能解决问题吗?

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

结果:

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

在此处查看您的结果:http://sqlfiddle.com/#!9/98ddd/4

【讨论】:

它给了我错误说“致命错误:在第 18 行调用布尔值上的成员函数 fetch()”,即“while ($row = $result->fetch())”跨度> 嗯,我给了你你问的答案,就像根据你给出的例子把两条线放在一起。现在你只需要调整你的 PHP 代码。 感谢您的帮助。解决不了这个问题我再问。

以上是关于如果具有相同的 id 和相同的值或值在 PHP 中为空,则合并行的主要内容,如果未能解决你的问题,请参考以下文章

当所有检索值在laravel中具有相同的会话键时,如何检查会话数组是不是具有唯一的ID

如何在具有相同 ID 的列中选择不同的值然后删除它们 PHP SQL Server

加入三行,如果一列中相同的值

如何用sql实现自动填充日期

PHP MySQL将来自不同行的单元格组合在一起具有相同的值

如果json对象键在javascript中具有所有相同的值,如何检查并返回true?