1

每个人。

我正在尝试在三个表上进行连接。这是完成此操作的代码:

$query = "SELECT Distinct ID, Date, SC_LName, SC_FName, Activity_Type, Hours, Pay_Rate, PT_First_Name, PT_Last_Name
FROM Payroll
LEFT OUTER JOIN Plans ON Plans.Client_ID = Payroll.Client_ID
GROUP BY STR_TO_DATE( DATE,  '%m/%d/%Y' ), Payroll.SC_LName";

$res = mysqli_query($con, $query);

查询后,我在 while 循环中有一个 for 循环,如下所示:

while ($row = mysqli_fetch_assoc($res)) {
    $daterange = $row['Approval_Date'];
    $m30 = date('m/d/Y',strtotime('+ 30 days', strtotime($daterange)));
    $m60 = date('m/d/Y',strtotime('+ 60 days', strtotime($daterange)));
    $m90 = date('m/d/Y',strtotime('+ 90 days', strtotime($daterange)));
    $m120 = date('m/d/Y',strtotime('+ 120 days', strtotime($daterange)));
    $m150 = date('m/d/Y',strtotime('+ 150 days', strtotime($daterange)));
    $m180 = date('m/d/Y',strtotime('+ 190 days', strtotime($daterange)));
    $m210 = date('m/d/Y',strtotime('+ 210 days', strtotime($daterange)));
    $m240 = date('m/d/Y',strtotime('+ 240 days', strtotime($daterange)));
    $m270 = date('m/d/Y',strtotime('+ 270 days', strtotime($daterange)));
    $m300 = date('m/d/Y',strtotime('+ 300 days', strtotime($daterange)));
    $m330 = date('m/d/Y',strtotime('+ 330 days', strtotime($daterange)));
    $m360 = date('m/d/Y',strtotime('+ 360 days', strtotime($daterange)));

    $Hourly_Pay = round($row['Pay_Rate'] * $row['Hours'],2);
    asort($row);
    foreach($row as $key => $val) {
        $time = strtotime($val);
        $newformat = date('Y-m-d',$time);
        $myformat = date('Y-m-d', strtotime($row['Date']));
            if ( $time > strtotime("01/28/2015") 
              AND $time < strtotime("03/31/2015") 
              AND $key <> "Date" 
              AND $myformat = $newformat) {
                 echo "<tr><td>" . $row['Date'];  
                 echo "</td><td>"; 
                 if (strtotime($row['Date']) < strtotime($m30)) {
                        echo "30 Day";
                 }; 
                 if (strtotime($row['Date']) > strtotime($m60) 
                    AND strtotime($row['Date']) < strtotime($m90)) {
                        echo "60 Day";
                 }; 
                 if (strtotime($row['Date']) > strtotime($m90) 
                    AND strtotime($row['Date']) < strtotime($m120)) {
                       echo "90-Day";
                 }; 
                 if (strtotime($row['Date']) > strtotime($m120) 
                    AND strtotime($row['Date']) < strtotime($m150)) {
                       echo "210 Day";
                 }; 
                 if (strtotime($row['Date']) > strtotime($m150) 
                    AND strtotime($row['Date']) < strtotime($m180)) {
                       echo "150-Day";
                 }; 
                 if (strtotime($row['Date']) > strtotime($m180) 
                    AND strtotime($row['Date']) < strtotime($m210)) {
                       echo "180-Day";
                 }; 
                 if (strtotime($row['Date']) > strtotime($m210) 
                     AND strtotime($row['Date']) < strtotime($m240)) {
                       echo "210 Day";
                 }; 
                 echo "</td><td>"  . $row['SC_LName'] . "</td><td>" 
                    . $row['SC_FName'] . "</td><td>" . $row['Title'] 
                    . "</td><td>" . $row['PT_Last_Name'] . "</td><td>" 
                    . $row['PT_First_Name'] . "</td><td>" 
                    . $row['Activity_Type'] . "</td><td>" 
                    . $row['Hours'] . "</td><td>" 
                    . $row['Pay_Rate'] . "</td><td>" 
                    . $Hourly_Pay .  "</td><td>" 
                    . $row['Budget_Amt'] . "</td><td>" 
                    . $row['Notes'] . "</td></tr>";
              }
      }
}

我的问题是我得到重复的行。行得到完全相同的数据。这不是预期的行为。预期的行为是每一行都有不同的东西。PT_LName/FName 应该不同,日期应该不同,或者 Activity_Type 应该不同。对于任何两行,它们都不应该都相同,但是我得到了每行的重复项。重复的数量似乎有所不同。

这是我第一次在这里发帖,所以如果您需要任何其他信息,请告诉我。我是新手。:)

编辑添加:我找到了解决方案,基于我在这里得到的慷慨帮助。:)

我将查询更改为:

$query = "SELECT * from Plans
LEFT OUTER JOIN Payroll ON Payroll.Client_ID = Plans.Client_ID";

我将我的 IF 语句更改为:

if ( $time > strtotime("01/28/2015") AND $time < strtotime("03/31/2015") AND $key == "Date" AND $myformat = $newformat) {echo "<tr><td>" . $row['Date'];  echo "</td><td>"; if (strtotime($row['Date']) < strtotime($m30)) {echo "30 Day";}; if (strtotime($row['Date']) > strtotime($m60) AND strtotime($row['Date']) < strtotime($m90)) {echo "60 Day";}; if (strtotime($row['Date']) > strtotime($m90) AND strtotime($row['Date']) < strtotime($m120)) {echo "90-Day";}; if (strtotime($row['Date']) > strtotime($m120) AND strtotime($row['Date']) < strtotime($m150)) {echo "210 Day";}; if (strtotime($row['Date']) > strtotime($m150) AND strtotime($row['Date']) < strtotime($m180)) {echo "150-Day";}; if (strtotime($row['Date']) > strtotime($m180) AND strtotime($row['Date']) < strtotime($m210)) {echo "180-Day";}; if (strtotime($row['Date']) > strtotime($m210) AND strtotime($row['Date']) < strtotime($m240)) {echo "210 Day";}; echo "</td><td>"  . $row['SC_LName'] . "</td><td>" . $row['SC_FName'] .  "</td><td>" . $row['PT_Last_Name'] . "</td><td>" . $row['PT_First_Name'] . "</td><td>" . $row['Activity_Type'] . "</td><td>" . $row['Hours'] . "</td><td>" . $row['Pay_Rate'] . "</td><td>" . $Hourly_Pay .  "</td><td>" . $row['Budget_Amt'] . "</td><td>" . $row['Notes'] . "</td></tr>";

总而言之,这两个更改为我提供了适量的行数。我在键/日期配对方面做错了,使连接变得过于复杂。

4

2 回答 2

0

基于我在这里得到的慷慨帮助,我找到了解决方案。:)

我将查询更改为:

$query = "SELECT * from Plans
LEFT OUTER JOIN Payroll ON Payroll.Client_ID = Plans.Client_ID";

我将我的 IF 语句更改为:

if ( $time > strtotime("01/28/2015") AND $time < strtotime("03/31/2015") AND $key == "Date" AND $myformat = $newformat) {echo "<tr><td>" . $row['Date'];  echo "</td><td>"; if (strtotime($row['Date']) < strtotime($m30)) {echo "30 Day";}; if (strtotime($row['Date']) > strtotime($m60) AND strtotime($row['Date']) < strtotime($m90)) {echo "60 Day";}; if (strtotime($row['Date']) > strtotime($m90) AND strtotime($row['Date']) < strtotime($m120)) {echo "90-Day";}; if (strtotime($row['Date']) > strtotime($m120) AND strtotime($row['Date']) < strtotime($m150)) {echo "210 Day";}; if (strtotime($row['Date']) > strtotime($m150) AND strtotime($row['Date']) < strtotime($m180)) {echo "150-Day";}; if (strtotime($row['Date']) > strtotime($m180) AND strtotime($row['Date']) < strtotime($m210)) {echo "180-Day";}; if (strtotime($row['Date']) > strtotime($m210) AND strtotime($row['Date']) < strtotime($m240)) {echo "210 Day";}; echo "</td><td>"  . $row['SC_LName'] . "</td><td>" . $row['SC_FName'] .  "</td><td>" . $row['PT_Last_Name'] . "</td><td>" . $row['PT_First_Name'] . "</td><td>" . $row['Activity_Type'] . "</td><td>" . $row['Hours'] . "</td><td>" . $row['Pay_Rate'] . "</td><td>" . $Hourly_Pay .  "</td><td>" . $row['Budget_Amt'] . "</td><td>" . $row['Notes'] . "</td></tr>";

总而言之,这两个更改为我提供了适量的行数。我在键/日期配对方面做错了,使连接变得过于复杂。

于 2015-03-20T16:31:23.237 回答
0

我怀疑您看到您认为重复行的原因是因为您在 SELECT 子句中包含 ID 和 PT_Last_Name 但从未在输出中使用它。

您可能想访问https://codereview.stackexchange.com/并寻求一些关于更好地构建代码的建议。

于 2015-03-20T13:46:29.690 回答