One-To-Many Join

  • Table A: contains a field "Name" and another field "Name2". The data types for these fields are ints.

    Table B: contains two fields: "NameID" and "ActualName".

    The fields "Name" and "Name2" from Table A need to match up with the "ActualName" from Table B.

    I need to create a view that has fields "Name1" and "Name2" that contain the value of the "ActualName" field in Table B.

    Any help would be appreciated.

  • a union query in the view should work

     

    select ActualName from TableA t1, TableB t2

    where t2.nameID = t1.Name

    union

    select ActualName from tableA t1, TableB t2

    where t2.nameID = t1.Name2

  • Would it be possible to obtain this data as one record, rather than 2 which is the case currently?

  • are nameid and actualname ints too?

    and anything is possible... but need more info to give you a solution...

    is their a common column, like do is theredo name and nameid match? is name2 and actualname the same, just different column names?

  • if i re-read what you asked for right, here is one possible solution:

    SELECT a.Name, b.ActualName [Name_Actual], b.NameID [Name_ID],

    a.name2, c.ActualName [Name2_Actual], c.NameID [Name2_ID]

    FROM TableA a

    LEFT JOIN TableB b

    ON a.name = b.ActualName

    LEFT JOIN TableB c

    ON a.name2 = c.ActualName

  • Try

    select t2.ActualName as Name1,t3.Actualname as Name2

    from TableA t1, TableB t2, TableB t3

    where t2.Nameid = t1.Name

    and t3.Nameid = t1.Name2

     

    or

     

    select t2.ActualName as Name1,t3.Actualname as Name2

    from TableA t1 join Tableb t2 on t2.Nameid = t1.Name

    join TableB t3 on t3.Nameid = t1.Name2

     

     

  • I actually figured it out. Actual name is a string in table B. Name1 and Name2 are ints in table A that referred to NameID in table B.

Viewing 7 posts - 1 through 6 (of 6 total)

You must be logged in to reply to this topic. Login to reply