SitePoint Sponsor

User Tag List

Results 1 to 3 of 3
  1. #1
    SitePoint Guru
    Join Date
    Oct 2011
    Posts
    658
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Problem in SELECT Statement

    i encountered problem in my select statement.

    this is my query:
    Code:
    SELECT a.EMP_ID, CONCAT(LASTNAME, ', ' , FIRSTNAME) AS FULLNAME, a.LOG_IN, a.LOG_OUT
     FROM hrdb.attendance AS a LEFT JOIN hris.employment em ON a.EMP_ID = em.EMP_NO 
     WHERE DATE(LOG_IN) BETWEEN '2011-12-08' AND '2011-12-23' 
     AND em.STATUS = 'Casual' OR LOG_IN IS NULL
    the output of this query is all data from attendance table was displayed eventhough their STATUS is not Casual.

    Thank you so much...

  2. #2
    From space with love silver trophy
    SpacePhoenix's Avatar
    Join Date
    May 2007
    Location
    Poole, UK
    Posts
    5,077
    Mentioned
    103 Post(s)
    Tagged
    0 Thread(s)
    try:

    Code SQL:
    SELECT
    a.EMP_ID
    , CONCAT(LASTNAME, ', ' , FIRSTNAME) AS FULLNAME
    , a.LOG_IN, a.LOG_OUT
     
    FROM
        hrdb.attendance AS a
    LEFT JOIN
        hris.employment AS em
            ON a.EMP_ID = em.EMP_NO
    WHERE
        ((DATE(LOG_IN) BETWEEN '2011-12-08' AND '2011-12-23')
            AND
                em.STATUS = 'Casual')
        OR
            LOG_IN IS NULL
    Community Team Advisor
    Forum Guidelines: Posting FAQ Signatures FAQ Self Promotion FAQ
    Help the Mods: What's Fluff? Report Fluff/Spam to a Moderator

  3. #3
    From Italy with love silver trophybronze trophy
    guido2004's Avatar
    Join Date
    Sep 2004
    Posts
    9,509
    Mentioned
    163 Post(s)
    Tagged
    4 Thread(s)
    If you only want the rows with em.STATUS = 'casual' then use INNER JOIN instead of LEFT JOIN.

    And when you use AND's and OR's in the WHERE clause, you'd better use brackets to prevend unexpected behaviour.
    Based on what you wrote, I think it should be:
    Code:
    SELECT a.EMP_ID, CONCAT(LASTNAME, ', ' , FIRSTNAME) AS FULLNAME, a.LOG_IN, a.LOG_OUT
    FROM hrdb.attendance AS a 
    INNER JOIN hris.employment em 
    ON a.EMP_ID = em.EMP_NO 
    WHERE (    DATE(LOG_IN) BETWEEN '2011-12-08' AND '2011-12-23' 
           OR    LOG_IN IS NULL )
    AND em.STATUS = 'Casual'


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •