【问题标题】:jdbc4.MySQLSyntaxErrorException: error in your SQL syntax; check the manual MySQL server version for the right syntax to use near ') )' at line 1jdbc4.MySQLSyntaxErrorException:您的 SQL 语法错误;检查手册 MySQL 服务器版本以在第 1 行的 ') )' 附近使用正确的语法
【发布时间】:2018-08-02 14:51:55
【问题描述】:

我在尝试使用 crudRepository 获取数据时遇到问题。 我正在使用本机查询从数据库中获取记录数,但出现以下错误。

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 ')  )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_171]

我在存储库中的查询是:

@Query(value = "select count(1) from user_records upr  where RECORD_ID in  ( SELECT record_id FROM region_record_relation where region_id in :regionIds) ", nativeQuery = true)
public int findAllRecordsforRegionIds(@Param("regionIds") List<Long> regionIds);

我不知道如何解决这个问题。我正在使用 Mysql 数据库。

show-sql 设置已打开。但我得到的只是:

 2018-08-02 20:06:17.210  WARN 3764 --- [nio-8088-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : SQL Error: 1064, SQLState: 42000
 2018-08-02 20:06:17.210 ERROR 3764 --- [nio-8088-exec-2] o.h.engine.jdbc.spi.SqlExceptionHelper   : 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 ')  )' at line 1
 2018-08-02 20:06:17.231 ERROR 3764 --- [nio-8088-exec-2] o.a.c.c.C.[.[.[/].[dispatcherServlet]    : Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is org.springframework.orm.jpa.JpaSystemException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet; nested exception is javax.persistence.PersistenceException: org.hibernate.exception.SQLGrammarException: could not extract ResultSet] with root cause

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 ')  )' at line 1
at sun.reflect.NativeConstructorAccessorImpl.newInstance0(Native Method) ~[na:1.8.0_171]
at sun.reflect.NativeConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_171]
at sun.reflect.DelegatingConstructorAccessorImpl.newInstance(Unknown Source) ~[na:1.8.0_171]

【问题讨论】:

  • 首先打印出生成的 SQL 查询字符串。
  • 尝试删除查询末尾的空格。

标签: java mysql spring spring-repositories


【解决方案1】:
@Query(value = "select count(1) from user_records upr  where RECORD_ID in  ( SELECT record_id FROM region_record_relation where region_id in :regionIds) 

应该是

@Query(value = "select count(1) from user_records upr  where RECORD_ID in  ( SELECT record_id FROM region_record_relation where region_id in (:regionIds))) 

有更好的方法来编写它会更有效。但这应该可以解决错误。

【讨论】:

  • 感谢它现在的工作。但我想知道为什么没有括号它就不起作用。在许多示例中,我看到没有括号蚂蚁它仍然有效..
猜你喜欢
  • 2016-03-04
  • 1970-01-01
  • 2013-11-22
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-05-31
  • 1970-01-01
相关资源
最近更新 更多