mybatis传输日期字符串sql诡异报错
Posted Arvin_Wong
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了mybatis传输日期字符串sql诡异报错相关的知识,希望对你有一定的参考价值。
sqlserver+mybatis
sql:
SELECT SODD.ITEM_ID
,DATEDIFF(MM,SOD.DOC_DATE,'2019-04-24') AS MONTH_DIFF
,SUM(SODD.BUSINESS_QTY) 下单数量
FROM SALES_ORDER_DOC SOD
LEFT JOIN SALES_ORDER_DOC_D SODD ON SOD.SALES_ORDER_DOC_ID=SODD.SALES_ORDER_DOC_ID
WHERE SOD.ApproveStatus='Y' AND DATEDIFF(MM,SOD.DOC_DATE,'2019-04-24')<6
GROUP BY SODD.ITEM_ID,DATEDIFF(MM,SOD.DOC_DATE,'2019-04-24')
sqlserver中运行sql没有问题
但是在mybatis中:
List<ProductScheduleDTO> productSchedule(@Param("deadLine") String deadLine,@Param("belong") String belong);
<select id="productSchedule" parameterType="string" resultMap="productScheduleMap">
SELECT SODD.ITEM_ID
,DATEDIFF(MM,SOD.DOC_DATE,#deadLine) AS MONTH_DIFF
,SUM(SODD.BUSINESS_QTY) 下单数量
FROM SALES_ORDER_DOC SOD
LEFT JOIN SALES_ORDER_DOC_D SODD ON SOD.SALES_ORDER_DOC_ID=SODD.SALES_ORDER_DOC_ID
WHERE SOD.ApproveStatus='Y' AND DATEDIFF(MM,SOD.DOC_DATE,#deadLine)<6
GROUP BY SODD.ITEM_ID,DATEDIFF(MM,SOD.DOC_DATE,#deadLine )
</select>
他就报错:
2019-04-24 16:14:00.963 DEBUG 10428 --- [io-8080-exec-10] c.e.i.m.ReportMapper.productSchedule : ==> Preparing: SELECT SODD.ITEM_ID ,DATEDIFF(MM,SOD.DOC_DATE,?) AS MONTH_DIFF ,SUM(SODD.BUSINESS_QTY) 下单数量 FROM SALES_ORDER_DOC SOD LEFT JOIN SALES_ORDER_DOC_D SODD ON SOD.SALES_ORDER_DOC_ID=SODD.SALES_ORDER_DOC_ID WHERE SOD.ApproveStatus='Y' AND DATEDIFF(MM,SOD.DOC_DATE,?)<6 GROUP BY SODD.ITEM_ID,DATEDIFF(MM,SOD.DOC_DATE,? )
2019-04-24 16:14:00.966 DEBUG 10428 --- [io-8080-exec-10] c.e.i.m.ReportMapper.productSchedule : ==> Parameters: 2019-04-24(String), 2019-04-24(String), 2019-04-24(String)
2019-04-24 16:14:01.003 ERROR 10428 --- [io-8080-exec-10] c.e.i.exception.GlobalExceptionHandler :
### Error querying database. Cause: com.microsoft.sqlserver.jdbc.SQLServerException: 选择列表中的列 'SALES_ORDER_DOC.DOC_DATE' 无效,因为该列没有包含在聚合函数或 GROUP BY 子句中。
### The error may exist in com/emc/icm/mapper/ReportMapper.xml
### The error may involve com.emc.icm.mapper.ReportMapper.productSchedule-Inline
### The error occurred while setting parameters
然后我复制了idea插件mybatis sql log日志工具中的sql到编译工具中执行,没毛病
到底哪里有问题啊。。。。。。
第二天。。解决了
把gourp by放在外层,第一次只做datediff获得月份差值,第二层再group by就搞定了
还是十分疑惑为什么,这或许是在提醒我该看源码了
以上是关于mybatis传输日期字符串sql诡异报错的主要内容,如果未能解决你的问题,请参考以下文章
MyBatis 诡异的Invalid bound statement not found 错误