java - jdbc4.MySQL语法错误异常: error in your SQL syntax; check the manual MySQL server version for the right syntax to use near ') )' at line 1

标签 java mysql spring spring-repositories

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

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]

最佳答案

@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))) 

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

关于java - jdbc4.MySQL语法错误异常: error in your SQL syntax; check the manual MySQL server version for the right syntax to use near ') )' at line 1,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51656559/

相关文章:

java - Spring security登录外部链接

java - 获取区域设置短代码

java - Spark GraphX 中完整图的分区策略

PHP 没有执行所有给定的语句

php - 我想用 mysqldump 备份表的最后记录

java - 如何在 Java 中创建 validator 来检查整数是否位于特定提供值的列表中

java - Spring 启动:运行抛出 ClassNotFoundException

java - 从 ActiveMQ DestinationSource.getQueues 响应正确迭代队列

java - 在 web.xml 文件中映射 WebSocketEndpoints

php - 具有多个where和join操作的Mysql Insert查询