追加查询无法使用Access 2016

时间:2018-05-10 09:35:15

标签: sql ms-access ms-access-2016

我正在为uni建立一个牙医预约系统。我已经构建了整个事情,直到昨天它工作正常。

我有一个追加查询,我想从tblOperation_Schedule追加Operation_ID,Treatment_ID和Patient_ID,其中Op_Complete = 1且且发票表中的Operation_ID已经不存在。

由于某种原因,行没有附加但我无法发现问题。我一直在试图想到它应该写的另一种方式,但我绝不是专家和连接看到问题。查询如下。

INSERT INTO tblInvoices 
( Treatment_ID, Operation_ID, Patient_ID )

SELECT 
tblOperation_Schedule.Treatment_ID, 
tblOperation_Schedule.Operation_ID, 
tblOperation_Schedule.Patient_ID

FROM tblOperation_Schedule 
INNER JOIN tblInvoices ON 
tblOperation_Schedule.Operation_ID = tblInvoices.Operation_ID
WHERE 
(((tblOperation_Schedule.Operation_ID) Not In (Select [Operation_ID] 
from tblInvoices)) 
AND 
(((tblOperation_Schedule.Op_Complete)="1"));

是否有人能够发现此问题或提出其他原因可能无法解决?谢谢!

1 个答案:

答案 0 :(得分:3)

您的问题是,您使用INNER JOIN制作了Operation_ID两个表格,然后在WHERE中,您检查tblOperation_Schedule.Operation_ID并非Operation_ID来自tblInvoices {1}}。因此,您的WHERE条件与您的JOIN条件冲突,并且不会返回任何行。

至少你应该将你的查询改为:

INSERT INTO tblInvoices 
    ( Treatment_ID, Operation_ID, Patient_ID )
SELECT Treatment_ID, 
    Operation_ID, 
    Patient_ID
FROM tblOperation_Schedule 
WHERE
    Operation_ID NOT IN (
        SELECT [Operation_ID] 
        FROM tblInvoices
    )
    AND Op_Complete = "1";