Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • World
  • Users
  • Groups
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse
Code Project
  1. Home
  2. Database & SysAdmin
  3. MySQL
  4. oddness with a left join query....

oddness with a left join query....

Scheduled Pinned Locked Moved MySQL
databasedesignquestion
3 Posts 2 Posters 14 Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • C Offline
    C Offline
    cjoki
    wrote on last edited by
    #1

    The code logic was designed to not have a record in the QuoteStatus table if the record was open, i.e. a new quote. Any other status on the quote was recorded in this status table like void, complete, etc... Why you ask? Well its not my design and I have to work with it. Regardless, I noticed an odd side effect of this design and was hoping someone can clear it up for me. I have one query as this...

    SELECT sq.QuoteID, sq.OrderTime, qs.Status
    FROM SalesQuote AS sq
    LEFT OUTER JOIN QuoteStatus AS qs ON sq.QuoteID = qs.QuoteID
    WHERE OrderTime < '2010-02-08'
    AND qs.Status!=3

    The query runs fine, and returns 2 rows of my test data. I should get 3 rows as one of the quotes is open and has no entry in the quotestatus table. I would expect to get a NULL value returned but the row is ommited. When I change to this query...

    SELECT sq.QuoteID, sq.OrderTime, qs.Status
    FROM SalesQuote AS sq
    LEFT OUTER JOIN QuoteStatus AS qs ON sq.QuoteID = qs.QuoteID
    WHERE OrderTime < '2010-02-08'

    will return 5 rows, one of which is the missing quote with a NULL status. Now in code logic I can just test and skip the records of a specific status, but I would prefer to not return the extra data when I do not need to. Any ideas..? There's a free cookie for the right answer! ;)

    G 1 Reply Last reply
    0
    • C cjoki

      The code logic was designed to not have a record in the QuoteStatus table if the record was open, i.e. a new quote. Any other status on the quote was recorded in this status table like void, complete, etc... Why you ask? Well its not my design and I have to work with it. Regardless, I noticed an odd side effect of this design and was hoping someone can clear it up for me. I have one query as this...

      SELECT sq.QuoteID, sq.OrderTime, qs.Status
      FROM SalesQuote AS sq
      LEFT OUTER JOIN QuoteStatus AS qs ON sq.QuoteID = qs.QuoteID
      WHERE OrderTime < '2010-02-08'
      AND qs.Status!=3

      The query runs fine, and returns 2 rows of my test data. I should get 3 rows as one of the quotes is open and has no entry in the quotestatus table. I would expect to get a NULL value returned but the row is ommited. When I change to this query...

      SELECT sq.QuoteID, sq.OrderTime, qs.Status
      FROM SalesQuote AS sq
      LEFT OUTER JOIN QuoteStatus AS qs ON sq.QuoteID = qs.QuoteID
      WHERE OrderTime < '2010-02-08'

      will return 5 rows, one of which is the missing quote with a NULL status. Now in code logic I can just test and skip the records of a specific status, but I would prefer to not return the extra data when I do not need to. Any ideas..? There's a free cookie for the right answer! ;)

      G Offline
      G Offline
      Graham Breach
      wrote on last edited by
      #2

      I think this is because NULL never matches anything - including qs.Status != 3 Try changing it to AND (qs.Status != 3 OR qs.Status IS NULL)

      C 1 Reply Last reply
      0
      • G Graham Breach

        I think this is because NULL never matches anything - including qs.Status != 3 Try changing it to AND (qs.Status != 3 OR qs.Status IS NULL)

        C Offline
        C Offline
        cjoki
        wrote on last edited by
        #3

        Graham, That was the magic bullet thanks! here is your cookie!

        1 Reply Last reply
        0
        Reply
        • Reply as topic
        Log in to reply
        • Oldest to Newest
        • Newest to Oldest
        • Most Votes


        • Login

        • Don't have an account? Register

        • Login or register to search.
        • First post
          Last post
        0
        • Categories
        • Recent
        • Tags
        • Popular
        • World
        • Users
        • Groups