SQL for Lookup tables

FreeriderUK

Well-known member
Joined
Jun 27, 2006
Messages
100
Location
London
Programming Experience
10+
In my Expenses table (Access DB), I have fields Category, Payee & Method

I also have a Category, Payee & Method table which are LOOKUP tables, all with ID and Description.

I need a SQL statement which will collect the descriptions of these three fields.

I can get the results from the Category table using the following:

VB.NET:
SELECT ExpenseID, Categories.CategoryDescription
FROM (Categories INNER JOIN Expenses ON Expenses.ExpenseCategory = Categories.categoryid)

but, can't seem to get my head round multiple tables.

How can I achieve the equivalent of:
VB.NET:
SELECT ExpenseID, Categories.CategoryDescription, ...
FROM (Categories INNER JOIN Expenses ON Expenses.ExpenseCategory = Categories.CategoryID)
(Payees INNER JOIN Expenses ON Expenses.ExpensePayee = Payees.PayeeID)
(Methods INNER JOIN Expenses ON Expenses.ExpenseMethod = Methods.MethodID)
?
 
VB.NET:
SELECT ExpenseID, Categories.CategoryDescription, ...
FROM (Categories INNER JOIN Expenses ON Expenses.ExpenseCategory = Categories.CategoryID)
(Payees INNER JOIN Expenses ON Expenses.ExpensePayee = Payees.PayeeID)
(Methods INNER JOIN Expenses ON Expenses.ExpenseMethod = Methods.MethodID)

VB.NET:
SELECT ExpenseID, Categories.CategoryDescription, ...
FROM
(
(
Categories INNER JOIN Expenses ON Expenses.ExpenseCategory = Categories.CategoryID
)
INNER JOIN Payees ON Expenses.ExpensePayee = Payees.PayeeID
)
INNER JOIN Methods ON Expenses.ExpenseMethod = Methods.MethodID

You obviously dont need the line spacing - I've just added it here to make it more legible.
 
Your expenses table is the centre of a star, right, and the lookup tables join as fingers to it?

it will be more understandable if you write your queries betteR:

VB.NET:
SELECT
  e.ID,
  c.CategoryText,
  p.PayeesText,
  m.MethodText
FROM
  Expenses e
  INNER JOIN
  Categories c 
  ON
    e.ExpenseCategory = Categories.CategoryID

  INNER JOIN
  Payees p
  ON 
    e.ExpensePayee = Payees.PayeeID

  INNER JOIN
  Methods m
  ON 
    e.ExpenseMethod = Methods.MethodID

Get it? you have ONE expenses table, and you stick 3 other tables to it. You do NOT write it out 3 times and attempt to stick one table each time
 

Latest posts

Back
Top