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. Database
  4. SQL Left Outer Join Problem

SQL Left Outer Join Problem

Scheduled Pinned Locked Moved Database
csharpasp-netdatabasecom
5 Posts 3 Posters 0 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.
  • V Offline
    V Offline
    Vimalsoft Pty Ltd
    wrote on last edited by
    #1

    i have Join that i expect to give me 3 Records but it gives me to Records . There is a Record in Table A and in table "C" there are two records , i used a left outer Join and i only get two records instead of 3. i dont want to use a Union to do this. Below is an Example

    CREATE TABLE #TABLEA
    (
    LIS_KEY VARCHAR(MAX) NULL,
    FUNC_KEY VARCHAR(MAX) NULL
    )

    INSERT INTO #TABLEA
    VALUES('047600055/R',NULL)

    CREATE TABLE #TABLEC
    (
    LIS_KEY VARCHAR(MAX) NULL,
    FUNC_KEY VARCHAR(MAX) NULL
    )

    INSERT INTO #TABLEC
    VALUES('047600055/R','GEOSS001')

    INSERT INTO #TABLEC
    VALUES('047600055/R','GEOSS002')

    SELECT * FROM #TABLEA A
    left outer JOIN #TABLEC C
    ON A.LIS_KEY = C.LIS_KEY
    WHERE A.LIS_KEY = '047600055/R'

    Thanks

    Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

    V Richard DeemingR 2 Replies Last reply
    0
    • V Vimalsoft Pty Ltd

      i have Join that i expect to give me 3 Records but it gives me to Records . There is a Record in Table A and in table "C" there are two records , i used a left outer Join and i only get two records instead of 3. i dont want to use a Union to do this. Below is an Example

      CREATE TABLE #TABLEA
      (
      LIS_KEY VARCHAR(MAX) NULL,
      FUNC_KEY VARCHAR(MAX) NULL
      )

      INSERT INTO #TABLEA
      VALUES('047600055/R',NULL)

      CREATE TABLE #TABLEC
      (
      LIS_KEY VARCHAR(MAX) NULL,
      FUNC_KEY VARCHAR(MAX) NULL
      )

      INSERT INTO #TABLEC
      VALUES('047600055/R','GEOSS001')

      INSERT INTO #TABLEC
      VALUES('047600055/R','GEOSS002')

      SELECT * FROM #TABLEA A
      left outer JOIN #TABLEC C
      ON A.LIS_KEY = C.LIS_KEY
      WHERE A.LIS_KEY = '047600055/R'

      Thanks

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

      V Offline
      V Offline
      Vimalsoft Pty Ltd
      wrote on last edited by
      #2

      Got the Solution

      SELECT *
      FROM
      (
      SELECT * FROM #TABLEA A
      UNION ALL
      SELECT * FROM #TABLEC C
      )t
      WHERE LIS_KEY = '047600055/R'

      Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

      M 1 Reply Last reply
      0
      • V Vimalsoft Pty Ltd

        Got the Solution

        SELECT *
        FROM
        (
        SELECT * FROM #TABLEA A
        UNION ALL
        SELECT * FROM #TABLEC C
        )t
        WHERE LIS_KEY = '047600055/R'

        Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

        M Offline
        M Offline
        Mycroft Holmes
        wrote on last edited by
        #3

        What happened to I don't want to use UNION :laugh:

        Never underestimate the power of human stupidity RAH

        V 1 Reply Last reply
        0
        • M Mycroft Holmes

          What happened to I don't want to use UNION :laugh:

          Never underestimate the power of human stupidity RAH

          V Offline
          V Offline
          Vimalsoft Pty Ltd
          wrote on last edited by
          #4

          i take back my words :laugh:

          Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

          1 Reply Last reply
          0
          • V Vimalsoft Pty Ltd

            i have Join that i expect to give me 3 Records but it gives me to Records . There is a Record in Table A and in table "C" there are two records , i used a left outer Join and i only get two records instead of 3. i dont want to use a Union to do this. Below is an Example

            CREATE TABLE #TABLEA
            (
            LIS_KEY VARCHAR(MAX) NULL,
            FUNC_KEY VARCHAR(MAX) NULL
            )

            INSERT INTO #TABLEA
            VALUES('047600055/R',NULL)

            CREATE TABLE #TABLEC
            (
            LIS_KEY VARCHAR(MAX) NULL,
            FUNC_KEY VARCHAR(MAX) NULL
            )

            INSERT INTO #TABLEC
            VALUES('047600055/R','GEOSS001')

            INSERT INTO #TABLEC
            VALUES('047600055/R','GEOSS002')

            SELECT * FROM #TABLEA A
            left outer JOIN #TABLEC C
            ON A.LIS_KEY = C.LIS_KEY
            WHERE A.LIS_KEY = '047600055/R'

            Thanks

            Vuyiswa Maseko, Spoted in Daniweb-- Sorry to rant. I hate websites. They are just wierd. They don't behave like normal code. C#/VB.NET/ASP.NET/SQL7/2000/2005/2008 http://www.vuyiswamaseko.com vuyiswa[at]dotnetfunda.com http://www.Dotnetfunda.com

            Richard DeemingR Offline
            Richard DeemingR Offline
            Richard Deeming
            wrote on last edited by
            #5

            Two rows would be the expected result for a LEFT JOIN with that data. Each row from the table on the left will be returned once for each matching row in the table on the right, or once if there are no matching rows. Since the table on the right only has two rows, and the table on the left only has one row, the result will be two rows. Jeff Atwood has a good visual explanation of SQL joins: http://blog.codinghorror.com/a-visual-explanation-of-sql-joins/[^] Another way to explain joins:

            • Product the Cartesian Product of the two tables - each row in the left-hand table is matched with each row in the right-hand table.
            • Remove the rows where the JOIN condition is not met.
            • Depending on the JOIN type:
              • INNER JOIN: Nothing to do.
              • LEFT (OUTER) JOIN: Any rows in the left-hand table but not in the results are added back, matched with a row of NULL values for the right-hand table.
              • RIGHT (OUTER) JOIN:: Any rows in the right-hand table but not in the results are added back, matched with a row of NULL values for the left-hand table.
              • FULL (OUTER) JOIN: Apply the rules for both LEFT and RIGHT joins.
            • Filter the result based on the conditions in the WHERE clause (if any).

            "These people looked deep within my soul and assigned me a number based on the order in which I joined." - Homer

            "These people looked deep within my soul and assigned me a number based on the order in which I joined" - Homer

            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