The problem is that you are trying to access tables that you haven't told the query about in both your WHERE
and SELECT
clauses. When you say Bill.BillID
in your SELECT
clause, you're saying that you want the Field BillID
from the table called Bill
.
The rule your syntax is breaking is that you can't use a table name in the SELECT
or WHERE
clauses, unless you've mentioned it in the FROM
clause (or a JOIN
).
So now, let's look at your query.
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice
LEFT OUTER JOIN Duty.JobID = Bill.JobID
WHERE Job.CustomerID = Customer.CustomerID
This means that you want to pick fields from the tables named Bill
and Duty
, and filter the results based on fields from the Job
and Customer
tables. However, you've only mentioned one table by name in the FROM
clause (Invoice
). You've almost joined the Duty
table, but you're joining it to a place in a table you haven't mentioned yet (Bill
). I'm going to guess that you intend to use Bill
as an alias for Invoice
. That would mean that what you really want is more like:
// Still incorrect
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice AS Bill
LEFT OUTER JOIN Duty ON Duty.JobID = Bill.JobID
WHERE Job.CustomerID = Customer.CustomerID
But we still haven't mentioned either the Job
or Customer
tables that are referenced in the WHERE
clause. In order to use them, you need to JOIN
those tables as well. We'd need to know more about your schema to figure out how to do that, but I can tell you that you'll need at least two more JOIN
clauses. Assuming that you have a CustomerID
field on the Invoice
table, we probably want to join like this.
// speculative
SELECT Bill.BillID, Duty.TaskTime
FROM Invoice AS Bill
LEFT OUTER JOIN Duty ON Duty.JobID = Bill.JobID
JOIN Customer ON Customer.CustomerID = Bill.CustomerID
JOIN Job ON // Well, something...We don't know.
I strongly recommend you spend some time reading and studying the excellent Wikipedia entry on SQL JOINs. Good luck~