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. Inner Join to match multiple rows in lookup table

Inner Join to match multiple rows in lookup table

Scheduled Pinned Locked Moved Database
questionregex
3 Posts 2 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.
  • R Offline
    R Offline
    redivider
    wrote on last edited by
    #1

    So. I'm trying to match multiple codes (in one record) to their descriptions as listed in another table. So I have... TABLE 1 with columns: Key, Code1, Code2 TABLE 2 with columns: Code, Description ...which are tables I have no control over. I want to do an inner join so that my new table will have columns... Key, Code1, Code1Description, Code2, Code2Description. Its easy enought to match one SELECT Table1.*, Table2.description AS Code1Description FROM Table1 INNER JOIN Table2 ON Table1.Code1=Table2.Code How do i expand this to 2 or 3 or 4 different codes??? Please?

    W 1 Reply Last reply
    0
    • R redivider

      So. I'm trying to match multiple codes (in one record) to their descriptions as listed in another table. So I have... TABLE 1 with columns: Key, Code1, Code2 TABLE 2 with columns: Code, Description ...which are tables I have no control over. I want to do an inner join so that my new table will have columns... Key, Code1, Code1Description, Code2, Code2Description. Its easy enought to match one SELECT Table1.*, Table2.description AS Code1Description FROM Table1 INNER JOIN Table2 ON Table1.Code1=Table2.Code How do i expand this to 2 or 3 or 4 different codes??? Please?

      W Offline
      W Offline
      WoutL
      wrote on last edited by
      #2

      The same way you did it for the first column. ie: adding a second would give:

      SELECT t1.*, c1.description AS Code1Description, c2.description AS Code2Description
      FROM Table1 t1
      INNER JOIN Table2 c1 ON t1.Code1=c1.Code
      INNER JOIN Table2 c2 ON t1.Code2=c2.Code

      With this you should be able to add as many code you would like.

      Wout Louwers

      R 1 Reply Last reply
      0
      • W WoutL

        The same way you did it for the first column. ie: adding a second would give:

        SELECT t1.*, c1.description AS Code1Description, c2.description AS Code2Description
        FROM Table1 t1
        INNER JOIN Table2 c1 ON t1.Code1=c1.Code
        INNER JOIN Table2 c2 ON t1.Code2=c2.Code

        With this you should be able to add as many code you would like.

        Wout Louwers

        R Offline
        R Offline
        redivider
        wrote on last edited by
        #3

        Thank you very much. Never would have got that on my own. But on some servers the description table is empty, and i'm not getting any return rows. How can i make it optional, to say Return row for key='123' and add the descriptions IF they exist in the descriptions table?

        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