运行SQL COUNT代码 - 缺少括号

时间:2012-01-04 21:33:46

标签: sql oracle select count sqlplus

我正在尝试运行此SQL命令,但它给出了一个SQL命令未正确结束的错误。注意,我使用的是Oracle的11g。

SELECT BookTitle.btName, BookTitle.Isbn FROM BookTitle AND SELECT COUNT(Isbn)
FROM BookTitle
JOIN Loan ON (Borrower.borId = Loan.borId)
JOIN BookCopy ON (Loan.bcId = BookCopy.bcId)
JOIN Authorship ON (BookCopy.isbn = Authorship.isbn)
JOIN Author ON (Author.authorID = Authorship.authorID)
WHERE Loan
ORDER BY Loan.dateOut DESC;

你能帮我指出什么是错的吗?

enter image description here

有几个表Book Title,BookCopy,Authorship,Loan,Author,Publisher和Borrower。我需要找到借阅最多的书并列出所有头衔和isbn。

BookTitle contains ISBN(PK), btName, datePublished, pubId*, ageLower, ageUpper, value.
Borrower contains borID(PK), borName, borAddress and borMaxbook
BOokCopy cvontains bcID(PK), ISBN*, dateAcquired, dateDestroyed
oan contains borID(PH)*, bcID(OK)*, dateOut(FK), dateDue and dateBack
Publisher contains PubId(PK), pubName and pubAddress
Publisher cotains PubID(8), pubName, pubAddress
Authoer contains authorID(PK) and authorName
Authorship contains authorId*, and ISBN*

3 个答案:

答案 0 :(得分:1)

试试这个:

SELECT BookTitle.btName, BookTitle.Isbn, COUNT(Isbn) FROM BookTitle
JOIN Loan ON (Borrower.borId = Loan.borId)
JOIN BookCopy ON (Loan.bcId = BookCopy.bcId)
JOIN Authorship ON (BookCopy.isbn = Authorship.isbn)
JOIN Author ON (Author.authorID = Authorship.authorID)
-- WHERE Loan (Commented out because its not ended properly)
ORDER BY Loan.dateOut DESC;

编辑:您的所有表格都显示错误。您的JOIN指的是FROM声明

中不存在的表格

EDIT2 :您的JOIN似乎是简单的连接,或者没有正确的语法更复杂的连接。请研究here关于JOIN类型的内容。对于简单连接,请使用此示例:

SELECT BookTitle.btName, BookTitle.Isbn, SELECT COUNT(Isbn) FROM BookTitle, Borrower, Loan, BookCopy, Authorship, Author
WHERE (Borrower.borId = Loan.borId)
AND (Loan.bcId = BookCopy.bcId)
AND (BookCopy.isbn = Authorship.isbn)
AND (Author.authorID = Authorship.authorID)
ORDER BY Loan.dateOut DESC;

编辑3 :检查herehere,了解有关如何正确构建联接的信息

答案 1 :(得分:1)

SELECT 
      BookTitle.btName  AS bookName
    , BookTitle.isbn    AS ISBN
    , COUNT(*)          AS numberOfLoans 
FROM 
        BookTitle  
    JOIN 
        BookCopy   ON BookCopy.isbn = BookTitle.isbn
    JOIN 
        Loan       ON Loan.bcId = BookCopy.bcId
    JOIN 
        Borrower   ON Borrower.borId = Loan.borId
GROUP BY 
      BookTitle.btName
    , BookTitle.isbn
ORDER BY 
      MAX(Loan.dateOut) DESC         --- order by latest loan

答案 2 :(得分:0)

问题似乎出现在第一行的AND和" Loan"在WHERE子句中......你到底想要做什么?