Shen Shen
Shen Shen

Reputation: 72

Join Table with 2 Field related to one field

I face some problems here and i need any helps

this is my table

TableA

uomid      | uomname
1            KG
2            Gram

TableB

id         | itemname      | uomid1   | uomid2
1            Corn            1          2

What i hope for result is

id         | itemname      | uomname1 | uomname2
1            Corn            KG         Gram

Thanks for any helps

Upvotes: 0

Views: 31

Answers (1)

Raging Bull
Raging Bull

Reputation: 18747

You need to join TableA twice with different alias names.

SELECT B.id,B.itemname,A1.uomname as uomname1,A2.uomname as uomname2
FROM TableB B JOIN
     TableA A1 ON A1.uomid=B.uomid1 JOIN
     TableA A2 ON A2.uomid=B.uomid2

Result:

ID  ITEMNAME    UOMNAME1    UOMNAME2
------------------------------------
1   Corn        KG          Gram

See result in SQL Fiddle.

Upvotes: 2

Related Questions