mysqlsyntaxerrorexception在mysql工作台上运行良好

1zmg4dgp  于 2021-06-21  发布在  Mysql
关注(0)|答案(3)|浏览(291)
String query = "select Books.BookId, Books.Title, Book_Loans.BranchId, count(Book_Loans.BookLoanId) as NoOfCopies from (Book_Loans join Books"
+ "on (Book_Loans.BookId = Books.BookId)) where Books.BookId = ? group by (Book_Loans.BranchId)";
        preparedStatement = connect.prepareStatement(query);
        preparedStatement.setLong(1, bookId);
        resultSet = preparedStatement.executeQuery();
        while(resultSet.next()) {
            System.out.println("BookId:"+resultSet.getLong(1)+"\nTitle:"+resultSet.getString(2)+"\nBranchId:"+resultSet.getLong(3)+"\nNoOfCopies:"+resultSet.getInt(4)+"\n");

我正在尝试执行上述代码段,但我得到mysqlsyntaxerrorexception,如下所示:

Exception in thread "main" com.mysql.jdbc.exceptions.jdbc4.MySQLSyntaxErrorException: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '(Book_Loans.BookId = Books.BookId)) where Books.BookId = 75 group by (Book_Loans' at line 1
    at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method)
    at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source)
    at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source)
    at java.lang.reflect.Constructor.newInstance(Unknown Source)
    at com.mysql.jdbc.Util.handleNewInstance(Util.java:425)
    at com.mysql.jdbc.Util.getInstance(Util.java:408)
    at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:944)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3976)
    at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3912)
    at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2530)
    at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2683)
    at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2486)
    at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1858)
    at com.mysql.jdbc.PreparedStatement.executeQuery(PreparedStatement.java:1966)
    at libraryModel.CopiesCheckedOutFromEachBranchForABook.numberOfCheckedOutCopies(CopiesCheckedOutFromEachBranchForABook.java:36)
    at libraryModel.Main.main(Main.java:47)

与查询相关的表如下:books(bookid,authorname,title,(publisher)name)book\u loans(bookloanid,dateout,duedate,branchid,bookid,cardno)
我试图通过请求用户输入branchid来显示从每个分支 checkout 的图书的副本数。当我在mysql工作台上运行这个查询时,它运行得很好,但是在eclipse上给了我语法错误。

h7appiyu

h7appiyu1#

sql查询不正确,您必须在books和on之间添加一个空格。另外,不需要再加上括号
用途:

String query = "select Books.BookId, Books.Title, Book_Loans.BranchId, count(Book_Loans.BookLoanId) as NoOfCopies from Book_Loans join Books on Book_Loans.BookId = Books.BookId where Books.BookId = ? group by Book_Loans.BranchId";
qvtsj1bj

qvtsj1bj2#

在“books”和“on”之间缺少一个空格。加上它你就没事了。

balp4ylt

balp4ylt3#

你的问题说不通。你说的是按分行汇总,然后包括图书信息。在大多数数据库中,甚至在mysql的最新版本中,这都会失败。
您的查询应该更像这样:

select bl.BranchId, count(*) as NumCopies
from Book_Loans bl join
     Books b
     on bl.BookId = b.BookId
where b.BookId = ? 
group by bl.BranchId;

如果您想包含关于这本书的信息,那么正确的方法是使用聚合函数或在 group by :

select bl.BranchId, count(*) as NumCopies, b.BookId, b.Title
from Book_Loans bl join
     Books b
     on bl.BookId = b.BookId
where b.BookId = ? 
group by bl.BranchId, b.BookId, b.Title;

注意,表别名使查询更易于编写和读取。
当然,一旦获得正确的查询,就需要在应用程序中正确地实现它,包括在查询行周围获得正确的间距。

相关问题