SQL statement works in Workbench but not in Java

浪尽此生 提交于 2019-12-12 00:44:53

问题


The SQL statement below works in mySQL Workbench, but when I execute it in Eclipse, there is an mySQL exeception error. 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 'by schedule.id' at line 1

String sqlStr = "select movie_db.movie , schedule.date , schedule.timeslot "
                        + ", schedule.seats as NoSeats,"
                        + " a.bookingsMade, if ( (schedule.seats-a.bookingsMade) is null, schedule.seats,(schedule.seats-a.bookingsMade) ) as availSeats"
                        + " ,schedule.movie_id, schedule.id as scID"
                        + " from schedule"
                        + " left outer join movie_db on ( movie_db.id=schedule.movie_id )"
                        + " left outer join ("
                        + " select count(*) as bookingsMade, tickets.movie_id as aid from tickets"
                        + " group by schedule_id"
                        + " ) as a on (schedule.id=a.aid)"
                        + " where schedule.movie_id=?"
                        + "group by schedule.id";

PreparedStatement pstmt = sqlConnect.getPreparedStatement(sqlStr);
pstmt.setInt(1, Integer.parseInt(movieId));

ResultSet rs = pstmt.executeQuery();

回答1:


that cannot work:

where schedule.movie_id=?"
                        + "group by schedule.id";

change it to

where schedule.movie_id=?"
                    + " group by schedule.id";


来源:https://stackoverflow.com/questions/17992685/sql-statement-works-in-workbench-but-not-in-java

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!