语法分析出错,不是 GROUP BY 表达式

报错

java 复制代码
### Cause: dm.jdbc.driver.DMException: 第 9 行, 第 69 列[30]附近出现错误: 
语法分析出错
; bad SQL grammar []; nested exception is dm.jdbc.driver.DMException: 第 9 行, 第 69 列[30]附近出现错误: 
语法分析出错
	at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:101) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:72) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81) ~[spring-jdbc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73) ~[mybatis-spring-1.3.2.jar:1.3.2]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446) ~[mybatis-spring-1.3.2.jar:1.3.2]
	at com.sun.proxy.$Proxy168.selectList(Unknown Source) ~[na:na]
	at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230) ~[mybatis-spring-1.3.2.jar:1.3.2]
	at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:139) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:76) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59) ~[mybatis-3.4.6.jar:3.4.6]
	at com.sun.proxy.$Proxy207.searchDimissionInfo(Unknown Source) ~[na:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
	at org.springframework.aop.support.AopUtils.invokeJoinpointUsingReflection(AopUtils.java:343) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.invokeJoinpoint(ReflectiveMethodInvocation.java:197) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.dao.support.PersistenceExceptionTranslationInterceptor.invoke(PersistenceExceptionTranslationInterceptor.java:139) ~[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.JdkDynamicAopProxy.invoke(JdkDynamicAopProxy.java:212) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at com.sun.proxy.$Proxy208.searchDimissionInfo(Unknown Source) ~[na:na]
	at com.chinaunicom.ihr.coreperson.service.PersonDimissionInfoService.sendQuestionnaire(PersonDimissionInfoService.java:82) ~[main/:na]
	at com.chinaunicom.ihr.coreperson.service.PersonDimissionInfoService$$FastClassBySpringCGLIB$$83f333ba.invoke(<generated>) ~[main/:na]
	at org.springframework.cglib.proxy.MethodProxy.invoke(MethodProxy.java:204) ~[spring-core-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$CglibMethodInvocation.invokeJoinpoint(CglibAopProxy.java:746) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:163) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.transaction.interceptor.TransactionAspectSupport.invokeWithinTransaction(TransactionAspectSupport.java:294) ~[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.transaction.interceptor.TransactionInterceptor.invoke(TransactionInterceptor.java:98) ~[spring-tx-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.ReflectiveMethodInvocation.proceed(ReflectiveMethodInvocation.java:185) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.aop.framework.CglibAopProxy$DynamicAdvisedInterceptor.intercept(CglibAopProxy.java:688) ~[spring-aop-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at com.chinaunicom.ihr.coreperson.service.PersonDimissionInfoService$$EnhancerBySpringCGLIB$$d4991145.sendQuestionnaire(<generated>) ~[main/:na]
	at com.chinaunicom.ihr.coreperson.web.QuestionAnswerInfoController.sendQuestionnaire(QuestionAnswerInfoController.java:40) ~[main/:na]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
	at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:209) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:136) ~[spring-web-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:102) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:891) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:797) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:87) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:991) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:925) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:974) ~[spring-webmvc-5.0.9.RELEASE.jar:5.0.9.RELEASE]
	... 68 common frames omitted
Caused by: dm.jdbc.driver.DMException: 第 9 行, 第 69 列[30]附近出现错误: 
语法分析出错
	at dm.jdbc.driver.DBError.throwException(DBError.java:657) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.a.b.p.D(MSG.java:582) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.a.b.p.A(MSG.java:542) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.a.b.p.z(MSG.java:523) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.a.a.a(DBAccess.java:795) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.a.a.a(DBAccess.java:213) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbPreparedStatement.prepareSql(DmdbPreparedStatement.java:199) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbPreparedStatement.allocateHandle(DmdbPreparedStatement.java:168) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbPreparedStatement.<init>(DmdbPreparedStatement.java:120) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbPreparedStatement.<init>(DmdbPreparedStatement.java:135) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbConnection.do_prepareStatement(DmdbConnection.java:696) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbConnection.do_prepareStatement(DmdbConnection.java:682) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at dm.jdbc.driver.DmdbConnection.prepareStatement(DmdbConnection.java:1280) ~[DmJdbcDriver18.jar:- 8.1.1.126 - Production]
	at com.alibaba.druid.pool.DruidPooledConnection.prepareStatement(DruidPooledConnection.java:349) ~[druid-1.1.9.jar:1.1.9]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
	at org.apache.ibatis.logging.jdbc.ConnectionLogger.invoke(ConnectionLogger.java:55) ~[mybatis-3.4.6.jar:3.4.6]
	at com.sun.proxy.$Proxy292.prepareStatement(Unknown Source) ~[na:na]
	at org.apache.ibatis.executor.statement.PreparedStatementHandler.instantiateStatement(PreparedStatementHandler.java:87) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.statement.BaseStatementHandler.prepare(BaseStatementHandler.java:88) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.statement.RoutingStatementHandler.prepare(RoutingStatementHandler.java:59) ~[mybatis-3.4.6.jar:3.4.6]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
	at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:63) ~[mybatis-3.4.6.jar:3.4.6]
	at com.sun.proxy.$Proxy291.prepare(Unknown Source) ~[na:na]
	at org.apache.ibatis.executor.SimpleExecutor.prepareStatement(SimpleExecutor.java:85) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:62) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:326) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:83) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148) ~[mybatis-3.4.6.jar:3.4.6]
	at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141) ~[mybatis-3.4.6.jar:3.4.6]
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) ~[na:1.8.0_281]
	at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) ~[na:1.8.0_281]
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) ~[na:1.8.0_281]
	at java.lang.reflect.Method.invoke(Method.java:498) ~[na:1.8.0_281]
	at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433) ~[mybatis-spring-1.3.2.jar:1.3.2]
	... 109 common frames omitted

出现问题的sql

sql 复制代码
        SELECT t1.person_id AS personId,
        t1.employee_number AS employeeNumber,
        t1.chinese_name AS chineseName,
        t1.org_name AS orgName,
        t1.job_name AS jobName,
        t1.grade_name AS gradeName,
        t1.dimission_time AS dimissionTime,
        t1.issue_time AS issueTime from person_dimission_info t1 GROUP BY
        t1.person_id HAVING COUNT(1)=1 AND date_sub(now(), interval 30 day)>issue_time

问题处理

首先问题很明显,是因为date_sub函数导致的

修改完了之后还是不行

sql 复制代码
SELECT t1.person_id       AS personId,
       t1.employee_number AS employeeNumber,
       t1.chinese_name    AS chineseName,
       t1.org_name        AS orgName,
       t1.job_name        AS jobName,
       t1.grade_name      AS gradeName,
       t1.dimission_time  AS dimissionTime,
       t1.issue_time      AS issueTime
from PERSON2.person_dimission_info t1
GROUP BY t1.person_id
HAVING COUNT(1) = 1
   AND add_days(now(), -30) > issue_time

然后就很明显了,这里的group by 分组应该是使用的oracle中的类似的方式,只有出现在group by 的列才可以出现在select之后。然后决定开始写子循环。

sql 复制代码
SELECT t1.person_id       AS personId
from PERSON2.person_dimission_info t1
GROUP BY t1.person_id
HAVING COUNT(1) = 1 and add_days(now(), -30) > t1.issue_time

然后出现了新的问题,我还以为是因为dm数据库不认having后面参数累加来着,因为之前处理过类似的问题。

sql 复制代码
SELECT t1.person_id       AS personId
from PERSON2.person_dimission_info t1
GROUP BY t1.person_id
HAVING COUNT(1) = 1 and 1=1

发现,其实他认having后面跟and,只不过我写的这个时间函数不认而已。

那没有办法,不能放在having,就放在where呗,一样的

sql 复制代码
SELECT t1.person_id       AS personId
from PERSON2.person_dimission_info t1
where    add_days(now(), -30) > t1.issue_time
GROUP BY t1.person_id
HAVING COUNT(1) = 1

最后写好的子循环

实话说我也不知道为啥不能够写=,我就换成了in

相关推荐
扫地僧-大白1 年前
Nacos2.1.2改造适配达梦数据库7.0
nacos·达梦数据库·dm数据库