表名 - scheme_master
column name - book_no2,receipt_no
表名 - book_issue
column name - book_no
表格名称 - book_return
column name - used_receipt
我只需要选择那些在图书发行表上创建图书的方案大师的那个receipt_no,以及book_return表中没有的receipt_no。
FOR EX -
scheme_master中的数据
Book_no2 receipt_no
1 - 10
1 - 11
2 - 14
2 - 15
3 - 18
3 - 19
DATE IN Book_issue表
Book_no
3
book used_receipt
3 18
book_return表格中的数据
在上面的例子中,有3本书在方案主表1,2,3中用diff创建。收据OK。
但在书籍发行表中,我们只分配了第3册。
AND在Book return table中,我们只使用了收据18。
所以我需要从方案主表中选择所有只有第3册的第19号收据......
-plz有助于解决这个问题......
以下是我的查询,它运作正常......
但这需要花费大量时间来选择记录......
FOR EX -it需要超过6分钟才能从30条记录中获取...
SELECT DISTINCT SM.receipt_no
FROM scheme_master SM
Inner join book_issue BI ON BI.book_no = SM.Book_no2
where
SM.receipt_no not in (select used_receipt from book_return)
答案 0 :(得分:0)
希望这会对你有所帮助。
SELECT sm.receipt_no FROM scheme_master as sm, book_return as br
INNER JOIN book_issue as bi ON sm.book_no2 = bi.book_no
WHERE sm.receipt_no != br.used_receipt
答案 1 :(得分:0)
我认为这就是你需要的......
SELECT scheme_master.Book_no2, scheme_master.receipt_no
FROM book_return RIGHT OUTER JOIN
Book_issue ON book_return.book = Book_issue.Book_no RIGHT OUTER JOIN
scheme_master ON Book_issue.Book_no = scheme_master.Book_no2
WHERE (Book_issue.Book_no IS NOT NULL) AND scheme_master.receipt_no != book_return.used_receipt
第二个选项获取所有记录,即使没有与书籍相关的记录......
SELECT scheme_master.Book_no2, scheme_master.receipt_no
FROM scheme_master LEFT OUTER JOIN
Book_issue ON Book_issue.Book_no = scheme_master.Book_no2 LEFT OUTER JOIN
book_return ON book_return.book = Book_issue.Book_no
WHERE (Book_issue.Book_no IS NOT NULL AND scheme_master.receipt_no <> book_return.used_receipt) OR
(book_return.used_receipt IS NULL)
最后 - 与你的例子类似 - 但运行起来并不多:
SELECT DISTINCT sm.Book_no2, sm.receipt_no
FROM scheme_master sm LEFT OUTER JOIN
Book_issue bi ON bi.Book_no = sm.Book_no2 LEFT OUTER JOIN
book_return br ON br.book = sm.Book_no2
WHERE sm.receipt_no NOT IN (SELECT DISTINCT used_receipt FROM book_return)