连接两个表,并将某些行数据表示为列

时间:2013-10-10 00:59:56

标签: php mysql

我有两个数据表,1表示员工数据,另一个表示员工工时。不幸的是,我收到了行格式的员工小时数据,我需要在员工数据旁边的列中表示......我正在努力...请看下面...

employeehours表:

  

ID -------- ------------- Paycode小时---- workdate

     

089999 01普通-------- 4.00 ----- 2013-09-16

     

089999 02加时赛1.5 --- 2.00 ----- 2013-09-16

     

089999 03加班2.0 --- 0.50 ----- 2013-09-16

     

083131 01普通-------- 7.60 ----- 2013-09-16

     

083131 02加时赛1.5 --- 0.43 ----- 2013-09-16

employeedata table:

  

ID ------姓氏 - 名字--- salarygrade
  ------------------- baserate --- otherrate

     

089999 SMITH ----- JOHN -------- XXX TWU EBA Perm Gr6 -   23.8508 ---- 0.0000


我想要一个查询来生成如下结果,我已经尝试了一切我的能力,但我不能让它工作......我知道我可能需要一些PHP编程来产生$值但是它也可以通过SQL做到这一点?非常感谢所有帮助...

  

ID ------姓氏 - 名字--- salarygrade
  ------------------- 01小时--- 01 $ ---- 02小时---- 02 $ --- 03小时-03 $

     

089999史密斯-----约翰-------- XXX TWU EBA Perm Gr6   --- 4.00 ---- 95.4032 - 2.00 ---- 71.5524--0.50--23.8508

对于某些具有ID的员工,某些数据可能不存在,并且在某些列中将以0.00表示......

这是我尝试过的:

    <?php
        $result1=mysql_query("SELECT `ID Number`, Surname, `First Name`, `Base Rate`, `Other Rate`, `Salary Code Description`, workdate, employeehours
                                FROM payroll.employeedatanew INNER JOIN payroll.employeehours ON employeedatanew.`ID Number` = employeehours.employeeid
                                GROUP BY Surname ORDER BY Surname Asc
                            ");

        $result2=mysql_query("  SELECT `ID Number`, paycode, workdate, employeehours
                                FROM payroll.employeedatanew INNER JOIN payroll.employeehours ON employeedatanew.`ID Number` = employeehours.employeeid
                                ORDER BY Surname Asc
                            ");


        while($show1=mysql_fetch_array($result1)){

        echo("<tr><td>".$show1['ID Number']."</td><td>".$show1['Surname']."</td><td>".$show1['First Name']."</td><td>".substr($show1['Salary Code Description'],11)."</td></tr>");

        }
        ?>
        </table>

        <table>
        <tr>

        <td>1.0x Hrs</td>
        <td>1.0x $</td>
        <!--<td>1.5x Hrs</td>
        <td>1.5x $</td>
        <td>2.0x Hrs</td>
        <td>2.0x $</td>
        <td>Crib Hrs</td>
        <td>Crib $</td>
        <td>Meal Hrs</td>
        <td>Meal $</td> -->

        </tr>
        <?php

        $resultset = array();
        while ($row = mysql_fetch_assoc($result3)) { $resultset[] = $row;  }

            while($show2=mysql_fetch_array($result2)){

            if ($show2['paycode'] == "01 Ordinary" && $show2['ID Number'] == $resultset['ID Number']) {
            echo ("<tr><td>".$show2['employeehours']."</td>");
            $normhourspay = ($show2['employeehours'] * $show3['Base Rate']);
            echo ("<td>".$normhourspay."</td></tr>");
            }           //else {echo("<tr><td>0</td><td>0</td></tr>");}}

和此:

    <table>
    <tr class="tabletitles">
        <td>Employee ID</td>
        <td>Surname</td>
        <td>First Name</td>
        <td>Pay Grade</td>
        <td>1.0x Hrs</td>
        <td>1.0x $</td>
        <!--<td>1.5x Hrs</td>
        <td>1.5x $</td>
        <td>2.0x Hrs</td>
        <td>2.0x $</td>
        <td>Crib Hrs</td>
        <td>Crib $</td>
        <td>Meal Hrs</td>
        <td>Meal $</td> -->

    </tr>

    <?php
        $result1=mysql_query("SELECT `ID Number`, Surname, `First Name`, `Base Rate`, `Other Rate`, `Salary Code Description`, workdate, employeehours
                                FROM payroll.employeedatanew INNER JOIN payroll.employeehours ON employeedatanew.`ID Number` = employeehours.employeeid
                                GROUP BY Surname ORDER BY Surname Asc
                            ");

        $result2=mysql_query("  SELECT `ID Number`, paycode, workdate, employeehours
                                FROM payroll.employeedatanew INNER JOIN payroll.employeehours ON employeedatanew.`ID Number` = employeehours.employeeid
                                ORDER BY Surname Asc
                            ");


        while($show1=mysql_fetch_array($result1)){

        echo("<tr><td>".$show1['ID Number']."</td><td>".$show1['Surname']."</td><td>".$show1['First Name']."</td><td>".substr($show1['Salary Code Description'],11)."</td></tr>");

        }
        echo ("</table>");

            while($show2=mysql_fetch_array($result2)){

            if ($show2['paycode'] == "01 Ordinary" && $show2['ID Number'] == $show1['ID Number']) {
            echo ("<td>".$show1['employeehours']."</td>");
            $normhourspay = ($show1['employeehours'] * $show1['Base Rate']);
            echo ("<td>".$normhourspay."</td></tr>");
            }           else {echo("<td>0</td><td>0</td>");}
                }   

请帮忙!

1 个答案:

答案 0 :(得分:0)

SELECT `ID Number`, Surname, `First Name`, `Salary Code Description`,
                                `Base Rate`, `Other Rate`,
                                MAX((case when employeehours.paycode = '01 Ordinary' then (employeehours.employeehours) end)) AS `1.0 Hours`,
                                MAX((case when employeehours.paycode = '02 Overtime 1.5' then (employeehours.employeehours) end)) AS `1.5 Hours`,
                                MAX((case when employeehours.paycode = '03 Overtime 2.0' then (employeehours.employeehours) end)) AS `2.0 Hours`,
                                MAX((case when employeehours.paycode = '78 Crib' then (employeehours.employeehours) end)) AS `Crib`,
                                MAX((case when employeehours.paycode = 'CZ Meal Allowance PS' then (employeehours.employeehours) end)) AS `Meal Allowance`,
                                MAX((case when employeehours.paycode = '86Y Sick with Cert' then (employeehours.employeehours) end)) AS `Sick with Cert`,
                                MAX((case when employeehours.paycode = '86N Sick without Cert' then (employeehours.employeehours) end)) AS `Sick without Cert`,
                                MAX((case when employeehours.paycode = '87 Sick without Pay' then (employeehours.employeehours) end)) AS `Sick without Pay`,
                                MAX((case when employeehours.paycode = '83 Annual Leave' then (employeehours.employeehours) end)) AS `Annual Leave`,
                                MAX((case when employeehours.paycode = '95 RDO Taken' then (employeehours.employeehours) end)) AS `RDO Taken`,
                                MAX((case when employeehours.paycode = '85 LSL' then (employeehours.employeehours) end)) AS `LSL`,
                                MAX((case when employeehours.paycode = '61 Shift 17.5' then (employeehours.employeehours) end)) AS `Shift 17.5`
                                FROM payroll.employeedatanew INNER JOIN payroll.employeehours ON employeedatanew.`ID Number` = employeehours.employeeid
                                Group By Surname
                                ORDER BY Surname asc, `1.0 Hours` desc, `1.5 Hours` desc, `2.0 Hours` desc

这就是诀窍,感谢大家观看:)