Multiple INNER JOIN from the same table

49,839

Solution 1

You should specify different aliases for your tables . you are calling all of them m.

SELECT m1.MetalCode as 'Metal1', m2.MetalCode as 'Metal2',m3.MetalCode as 'Metal3'
FROM Item as k
INNER JOIN Metals AS m1 ON m1.metalID=k.metal1 
INNER JOIN Metals AS m2 ON m2.metalID=k.metal2
INNER JOIN Metals AS m3 ON m3.metalID=k.metal3
WHERE k.ItemID=?

Solution 2

Well, not completely wrong. ;)

Wherever you have "INNER JOIN Metals AS m", m needs to be something unique (not m every time).

Try something like this (not tested):

SELECT m1.MetalCode as 'Metal1', m2.MetalCode as 'Metal2', m3.MetalCode as 'Metal3'
FROM Item as k
INNER JOIN Metals AS m1 ON m1.metalID=k.metal1 
INNER JOIN Metals AS m2 ON m2.metalID=k.metal2
INNER JOIN Metals AS m3 ON m3.metalID=k.metal3
WHERE k.ItemID=?

Solution 3

try this:

SELECT m.MetalCode as 'Metal1', n.MetalCode as 'Metal2'o.MetalCode as 'Metal3'
FROM Item as k INNER JOIN Metals AS m ON m.metalID=k.metal1 
        INNER JOIN Metals AS n ON n.metalID=k.metal2
        INNER JOIN Metals AS o ON o.metalID=k.metal3
WHERE k.ItemID=?

Solution 4

SELECT m1.MetalCode as 'Metal1', m2.MetalCode as 'Metal2',m3.MetalCode as 'Metal3'
FROM Item as k
INNER JOIN Metals AS m1 ON m1.metalID=k.metal1 
INNER JOIN Metals AS m2 ON m2.metalID=k.metal2
INNER JOIN Metals AS m3 ON m3.metalID=k.metal3
WHERE k.ItemID=?

or simpler but getting one metalcode per row

SELECT MetalCode
FROM Item
WHERE metalID = metal1 OR metalID = metal2 OR metalID = metal3
Share:
49,839

Related videos on Youtube

NCFUSN
Author by

NCFUSN

I like spending my time coding, designing app architecture, and building APIs, as well as studying new technologies and programming languages. I revel in new and bizarre things. This is all what I really like. All the rest doesn’t matter.

Updated on April 06, 2020

Comments

  • NCFUSN
    NCFUSN about 4 years

    I have a table of metals

    MetalID    integer
    MetalName  text
    MetalCode  text
    

    Item table

    ItemID     integer
    ItemName   text
    ...
    Metal1     int Ref.-> metals.metalID
    Metal2     int Ref.-> metals.metalID
    Metal3     int Ref.-> metals.metalID
    

    I am trying to select three MetalCodes

    SELECT m.MetalCode as 'Metal1', m.MetalCode as 'Metal2',m.MetalCode as 'Metal3'
    FROM Item as k
    INNER JOIN Metals AS m ON m.metalID=k.metal1 
    INNER JOIN Metals AS m ON m.metalID=k.metal2
    INNER JOIN Metals AS m ON m.metalID=k.metal3
    WHERE k.ItemID=?
    

    Looks like I am doing it completely wrong. Please, help.

    • rid
      rid about 12 years
      You can't have multiple aliases with the same name.
    • YXD
      YXD about 12 years
      What are you trying to achieve?
  • Dagg Nabbit
    Dagg Nabbit about 12 years
    nice, we wrote exactly the same example code at exactly the same time. Must be twins. +1, in fact :)
  • NCFUSN
    NCFUSN about 12 years
    Theres still something wrong. I am getting result -> Zn,Zn,Zn while there's different metals
  • Dagg Nabbit
    Dagg Nabbit about 12 years
    @Nathan Considering we wrote these answers in a matter of seconds without testing, that's extremely probable. These examples should get you headed in the right direction though.
  • Beatles1692
    Beatles1692 about 12 years
    maybe something is wrong with your data please check to see whether k.metail1,k.metal2,k.metal3 having the same value ?
  • NCFUSN
    NCFUSN about 12 years
    Yes, that was the real answer-boom. 7 answers in a couple of seconds.
  • NCFUSN
    NCFUSN about 12 years
    4 - Zinc, 1-UNDEF. So, logically the result should be Zinc,UNDEF,UNDEF
  • Beatles1692
    Beatles1692 about 12 years
    are you using m1.MetalCode,m2.MetalCode and m3.MetalCode in your select list ?