SQL statement works in Workbench but not in Java

2019-09-06 10:08发布

问题:

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";