How to join tables?

I have two tables -
Item
- ItemID
- ItemName
- SalesTaxCodeID
- PurchaseTaxCodeID

TaxCode
- TaxCodeID
- Rate

In table Item, I have field SalesTaxCodeID and PurchaseTaxCodeID.  Is it possible to select from the two tables so that I can get recordset of columns ItemName, SalesTaxCodeRate, PurchaseTaxCodeRate

Thanks



Answer this question

How to join tables?

  • roberto mariconda

    select i.ItemName, tcsales.Rate as SalesTaxCodeRate, tcpurchase.Rate as PurchaseTaxCodeRate
    from Item i
    inner join TaxCode tcsales on (tcsales.TaxCodeID = i.SalesTaxCodeID)
    inner join TaxCode tcpurchase on (tcpurchase.TaxCodeID = i.PurchaseTaxCodeID)

    If the TaxCodeIDs in Item are nullable you need to change inner join to left outer join


  • How to join tables?