Problem in WHERE Clause from SELECT Statement

Discussion in 'MySQL' started by newphpcoder, Dec 13, 2011.

  1. #1
    Hi...

    I have a SELECT Statement to get the Rate and Hours per employee.

    
    $sql = "SELECT em.EMP_NO, em.STATUS, w.RATE, r.Hours 
    FROM $ADODB_DB.employment AS em 
    INNER JOIN $ADODB_DB.wage AS w ON em.EMP_ID = w.EMP_ID
    LEFT JOIN $PAYROLL.regular_sum_hours AS r ON em.EMP_NO = r.EMP_NO
    WHERE  em.EMP_ID = '$currentEmpID'";
    $RsEarnings = $conn2->Execute($sql); 
    
      $Rate      = $RsEarnings->fields['RATE'];
      $Hours      = $RsEarnings->fields['Hours'];
    
      $Hours = substr($Hours, 0, 5);
      $Hours = str_replace(':', '.', $Hours);
      
      
     $Amount = $_POST["Amount"];
     
     $Amount = round(($Hours/8)* $Rate, 2); 
    
    Code (markup):
    and Now I revised it, because I need to add OT_Hours in Hours where the STATUS = 'OffSet'.

    here is the revised code:
    
    $sql = "SELECT em.EMP_NO, em.STATUS, w.RATE, r.Hours, o.OT_Hours, o.STATUS 
    FROM $ADODB_DB.employment AS em 
    INNER JOIN $ADODB_DB.wage AS w ON em.EMP_ID = w.EMP_ID
    LEFT JOIN $PAYROLL.regular_sum_hours AS r ON em.EMP_NO = r.EMP_NO
    LEFT JOIN $PAYROLL.ot_data AS o ON r.EMP_NO = o.EMP_NO
    WHERE  em.EMP_ID = '$currentEmpID' AND o.STATUS = 'OffSet'";
    $RsEarnings = $conn2->Execute($sql); 
    
      $Rate      = $RsEarnings->fields['RATE'];
      $Hours      = $RsEarnings->fields['Hours'];
      $Offset = $RsEarnings->fields['OT_Hours'];
    
      $Hours = substr($Hours, 0, 5);
      $Hours = str_replace(':', '.', $Hours);
      
      $Hours = ($Hours + $Offset);
      
     $Amount = $_POST["Amount"];
     
     $Amount = round(($Hours/8)* $Rate, 2); 
    
    Code (markup):
    When I run this revised code I noticed that if the employee has no data in ot_data OT_Hours where STATUS = 'OffSet' the Rate and Hours will not displayed.

    I want it even the employee has no data on ot_data the Rate and Hours will still display..

    I'm still find the solution for that.

    Any help is highly appreciated..

    Thank you
     
    newphpcoder, Dec 13, 2011 IP
  2. Rukbat

    Rukbat Well-Known Member

    Messages:
    2,908
    Likes Received:
    37
    Best Answers:
    51
    Trophy Points:
    125
    #2
    Try

    RIGHT JOIN $PAYROLL.ot_data ...
     
    Rukbat, Dec 15, 2011 IP
  3. newphpcoder

    newphpcoder Greenhorn

    Messages:
    57
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    16
    #3
    i already resolved by adding condition in ON:
    $sql = "SELECT em.EMP_NO, em.STATUS, w.RATE, r.Hours, o.OT_Hours, o.STATUS FROM $ADODB_DB.employment AS em INNER JOIN $ADODB_DB.wage AS w ON em.EMP_ID = w.EMP_IDLEFT JOIN $PAYROLL.regular_sum_hours AS r ON em.EMP_NO = r.EMP_NOLEFT JOIN $PAYROLL.ot_data AS o ON ( r.EMP_NO = o.EMP_NO AND o.STATUS = 'OffSet' )WHERE  em.EMP_ID = '$currentEmpID'";
    Code (markup):
    Thank you
     
    newphpcoder, Dec 15, 2011 IP