1

我正在尝试使用 JOIN TABLE QUERY 显示为客户完成的工作的所有相关信息。

这是我的代码...

SELECT Bill.BillID, Duty.TaskTime 
FROM Invoice 
LEFT OUTER JOIN Duty.JobID = Bill.JobID
WHERE Job.CustomerID = Customer.CustomerID

我不断收到此错误...'#1064 - 您的 SQL 语法有错误;检查与您的 MySQL 服务器版本相对应的手册,以在第 3 行的“.JobID WHERE Job.CustomerID = Customers.CustomerID”附近使用正确的语法

4

2 回答 2

1

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~

于 2013-06-26T19:31:14.547 回答
0

你的错误是你如何指向你的桌子,也许:

FROM Invoice as Bill LEFT OUTER JOIN NameTable2 as Duty ON Dury.JobID=Bill.JobID

于 2013-06-26T18:25:45.493 回答