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. Query Problem ( INNER JOIN)

Query Problem ( INNER JOIN)

Scheduled Pinned Locked Moved Database
helpdatabasequestion
4 Posts 4 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.
  • M Offline
    M Offline
    mrkeivan
    wrote on last edited by
    #1

    Hey, Guys Let me just lay out the problem: I have 2 tables table Function FID FName Table Materials MID MName FunctionID1 (foriegn key for FID) FunctionID2 (foriegn key for FID) Now I want to select all columns from Materials table, instead of FunctionID1 and FunctionID2, I want to have their related text (FName).

    SELECT M.MID, M.MName, F.FName, F.Fname FROM Materials AS M INNER JOIN Function AS F
    ON FunctionID1=F.FID OR FunctionID2=F.FID
    WHERE FunctionID1 = X(input) and FunctionID2 = X(input)

    I want this query to search for records in which FunctionID 1 is The input or FunctionID2 is equal to related input!!!! buy when I do this, It fetches two rows for a row and in each one FunctionID1 and FunctionID2 are the same ! How should I fix this???

    M S B 3 Replies Last reply
    0
    • M mrkeivan

      Hey, Guys Let me just lay out the problem: I have 2 tables table Function FID FName Table Materials MID MName FunctionID1 (foriegn key for FID) FunctionID2 (foriegn key for FID) Now I want to select all columns from Materials table, instead of FunctionID1 and FunctionID2, I want to have their related text (FName).

      SELECT M.MID, M.MName, F.FName, F.Fname FROM Materials AS M INNER JOIN Function AS F
      ON FunctionID1=F.FID OR FunctionID2=F.FID
      WHERE FunctionID1 = X(input) and FunctionID2 = X(input)

      I want this query to search for records in which FunctionID 1 is The input or FunctionID2 is equal to related input!!!! buy when I do this, It fetches two rows for a row and in each one FunctionID1 and FunctionID2 are the same ! How should I fix this???

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

      If the content of the rows is the same DISTINCT might be useful. Using OR in a join statement is always going to cause you problems. You may also want to use sub selects, do your initial filtering in the from (select) Select * from (Select * from Function where Id1 = @ID1) X

      Never underestimate the power of human stupidity RAH

      1 Reply Last reply
      0
      • M mrkeivan

        Hey, Guys Let me just lay out the problem: I have 2 tables table Function FID FName Table Materials MID MName FunctionID1 (foriegn key for FID) FunctionID2 (foriegn key for FID) Now I want to select all columns from Materials table, instead of FunctionID1 and FunctionID2, I want to have their related text (FName).

        SELECT M.MID, M.MName, F.FName, F.Fname FROM Materials AS M INNER JOIN Function AS F
        ON FunctionID1=F.FID OR FunctionID2=F.FID
        WHERE FunctionID1 = X(input) and FunctionID2 = X(input)

        I want this query to search for records in which FunctionID 1 is The input or FunctionID2 is equal to related input!!!! buy when I do this, It fetches two rows for a row and in each one FunctionID1 and FunctionID2 are the same ! How should I fix this???

        S Offline
        S Offline
        Syed Mehroz Alam
        wrote on last edited by
        #3

        How about using two joins like this:

        SELECT M.MID, M.MName, F1.FName, F2.Fname
        FROM Materials AS M
        INNER JOIN Function AS F1 ON M.FunctionID1=F1.FID
        INNER JOIN Function AS F2 ON M.FunctionID2=F2.FID
        WHERE FunctionID1 = X(input) and FunctionID2 = X(input)

        Regards, Syed Mehroz Alam

        My Blog My Articles Computers are incredibly fast, accurate, and stupid; humans are incredibly slow, inaccurate and brilliant; together they are powerful beyond imagination. - Albert Einstein

        1 Reply Last reply
        0
        • M mrkeivan

          Hey, Guys Let me just lay out the problem: I have 2 tables table Function FID FName Table Materials MID MName FunctionID1 (foriegn key for FID) FunctionID2 (foriegn key for FID) Now I want to select all columns from Materials table, instead of FunctionID1 and FunctionID2, I want to have their related text (FName).

          SELECT M.MID, M.MName, F.FName, F.Fname FROM Materials AS M INNER JOIN Function AS F
          ON FunctionID1=F.FID OR FunctionID2=F.FID
          WHERE FunctionID1 = X(input) and FunctionID2 = X(input)

          I want this query to search for records in which FunctionID 1 is The input or FunctionID2 is equal to related input!!!! buy when I do this, It fetches two rows for a row and in each one FunctionID1 and FunctionID2 are the same ! How should I fix this???

          B Offline
          B Offline
          Ben Fair
          wrote on last edited by
          #4

          Actually, I think you'd want to use two LEFT OUTER JOINS, as you'll want to return a match on FunctionID1 or FunctionID2 (unless I've read this all incorrectly):

          select M.MID, M.MName, F1.FName [F1Name], F2.FName [F2Name]
          from Materials M
          left outer join Functions F1 on F1.FID = M.Function1ID
          left outer join Functions F2 on F2.FID = M.Function2ID
          where F1.FID is not null or F2.FID is not null

          The WHERE clause will elimitate records where neither the Function1ID nor Function2ID match the input value. And the double LEFT OUTER JOINs will cause records where either the Function1ID or Function2ID match the supplied input value to be pulled.

          Hope in one hand and poop in the other; see which fills up first. Hope and change were good slogans, now show us more than words.

          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