解决使用UNIO关键字报错:The used SELECT statements have a different number of columns的方案

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/erlian1992/article/details/80328823
解决使用UNIO关键字报错:The used SELECT statements have a different number of columns的方案

错误描述:
异常信息:
2018-05-15 16:50:08 [http-nio-8103-exec-8] ERROR com.huajin.queryserver.advice.ExpectionAdviceController - Handle request fail!
org.springframework.jdbc.BadSqlGrammarException:
### Error querying database. Cause: java.sql.SQLException: The used SELECT statements have a different number of columns
### The error may exist in file [/data/App/queryserver.cfaoe.local/mybatis/mappings/assetservice/AssertBalQueryMapper.xml]
### The error may involve com.huajin.queryserver.dao.assetservice.AssetQueryDao.findUserAssetDetail-Inline
### The error occurred while setting parameters
### SQL: SELECT count(0) FROM (SELECT p.MemberId AS MemeberId, p.ProjectFullName AS ProjectFullName, CfmMoney AS changeMoney, CfmTime AS updateTime, 1 AS bizType FROM biz_exchange_qa.bizmoney_cfmright_detail c JOIN biz_exchange_qa.bizorder_apply o ON (c.OrderApplyId = o.Id) JOIN biz_reg_listing.listing_base p ON (c.ProjectId = p.Id) WHERE o.OrderUserId = ? AND c.CfmStatus IN (2, 3) UNION ALL SELECT p.ProjectFullName, DrawBackMoney, PayResultTime, 2 FROM biz_exchange_qa.bizmoney_drawback_detail d JOIN biz_exchange_qa.bizmoney_cfmright_detail c ON (d.CfmRightDetailId = c.Id) JOIN biz_exchange_qa.bizorder_apply o ON (c.OrderApplyId = o.Id) JOIN biz_reg_listing.listing_base p ON (d.ProjectId = p.Id) WHERE o.OrderUserId = ? AND d.PayStatus = 2 UNION ALL SELECT p.ProjectFullName, PayInvestMoney, r.UpdateTime, 3 FROM biz_exchange_qa.bizpayinvest_detail_result r JOIN biz_reg_listing.listing_base p ON (r.ProjectId = p.Id) WHERE InvestUserId = ? AND r.PayInvestStatus = 4) table_count
### Cause: java.sql.SQLException: The used SELECT statements have a different number of columns
; bad SQL grammar []; nested exception is java.sql.SQLException: The used SELECT statements have a different number of columns
at org.springframework.jdbc.support.SQLStateSQLExceptionTranslator.doTranslate(SQLStateSQLExceptionTranslator.java:99)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:73)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.springframework.jdbc.support.AbstractFallbackSQLExceptionTranslator.translate(AbstractFallbackSQLExceptionTranslator.java:81)
at org.mybatis.spring.MyBatisExceptionTranslator.translateExceptionIfPossible(MyBatisExceptionTranslator.java:73)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:446)
at com.sun.proxy.$Proxy132.selectList(Unknown Source)
at org.mybatis.spring.SqlSessionTemplate.selectList(SqlSessionTemplate.java:230)
at org.apache.ibatis.binding.MapperMethod.executeForMany(MapperMethod.java:137)
at org.apache.ibatis.binding.MapperMethod.execute(MapperMethod.java:75)
at org.apache.ibatis.binding.MapperProxy.invoke(MapperProxy.java:59)
at com.sun.proxy.$Proxy133.findUserAssetDetail(Unknown Source)
at com.huajin.queryserver.service.assetservice.AssetQueryService.findUserAssetDetail(AssetQueryService.java:44)
at com.huajin.queryserver.service.assetservice.AssetQueryRootService.findUserAssetDetailByPage(AssetQueryRootService.java:50)
at com.huajin.queryserver.controller.AssetQueryController.findUserAssetDetailByPage(AssetQueryController.java:67)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:205)
at org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:133)
at org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:116)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:827)
at org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.handleInternal(RequestMappingHandlerAdapter.java:738)
at org.springframework.web.servlet.mvc.method.AbstractHandlerMethodAdapter.handle(AbstractHandlerMethodAdapter.java:85)
at org.springframework.web.servlet.DispatcherServlet.doDispatch(DispatcherServlet.java:963)
at org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:897)
at org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:970)
at org.springframework.web.servlet.FrameworkServlet.doPost(FrameworkServlet.java:872)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:648)
at org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:846)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:729)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:230)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.tomcat.websocket.server.WsFilter.doFilter(WsFilter.java:52)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.web.filter.ApplicationContextHeaderFilter.doFilterInternal(ApplicationContextHeaderFilter.java:55)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.trace.WebRequestTraceFilter.doFilterInternal(WebRequestTraceFilter.java:108)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.RequestContextFilter.doFilterInternal(RequestContextFilter.java:99)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HttpPutFormContentFilter.doFilterInternal(HttpPutFormContentFilter.java:105)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.HiddenHttpMethodFilter.doFilterInternal(HiddenHttpMethodFilter.java:81)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.cloud.sleuth.instrument.web.TraceFilter.doFilter(TraceFilter.java:145)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.web.filter.CharacterEncodingFilter.doFilterInternal(CharacterEncodingFilter.java:197)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.springframework.boot.actuate.autoconfigure.MetricsFilter.doFilterInternal(MetricsFilter.java:106)
at org.springframework.web.filter.OncePerRequestFilter.doFilter(OncePerRequestFilter.java:107)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:192)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:165)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:198)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:96)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:474)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:140)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:79)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:87)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:349)
at org.apache.coyote.http11.Http11Processor.service(Http11Processor.java:783)
at org.apache.coyote.AbstractProcessorLight.process(AbstractProcessorLight.java:66)
at org.apache.coyote.AbstractProtocol$ConnectionHandler.process(AbstractProtocol.java:798)
at org.apache.tomcat.util.net.NioEndpoint$SocketProcessor.doRun(NioEndpoint.java:1434)
at org.apache.tomcat.util.net.SocketProcessorBase.run(SocketProcessorBase.java:49)
at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
at org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.sql.SQLException: The used SELECT statements have a different number of columns
at com.mysql.jdbc.SQLError.createSQLException(SQLError.java:957)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3878)
at com.mysql.jdbc.MysqlIO.checkErrorPacket(MysqlIO.java:3814)
at com.mysql.jdbc.MysqlIO.sendCommand(MysqlIO.java:2478)
at com.mysql.jdbc.MysqlIO.sqlQueryDirect(MysqlIO.java:2625)
at com.mysql.jdbc.ConnectionImpl.execSQL(ConnectionImpl.java:2551)
at com.mysql.jdbc.PreparedStatement.executeInternal(PreparedStatement.java:1861)
at com.mysql.jdbc.PreparedStatement.execute(PreparedStatement.java:1192)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2931)
at com.alibaba.druid.filter.FilterEventAdapter.preparedStatement_execute(FilterEventAdapter.java:440)
at com.alibaba.druid.filter.FilterChainImpl.preparedStatement_execute(FilterChainImpl.java:2929)
at com.alibaba.druid.proxy.jdbc.PreparedStatementProxyImpl.execute(PreparedStatementProxyImpl.java:131)
at com.alibaba.druid.pool.DruidPooledPreparedStatement.execute(DruidPooledPreparedStatement.java:493)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.apache.ibatis.logging.jdbc.PreparedStatementLogger.invoke(PreparedStatementLogger.java:59)
at com.sun.proxy.$Proxy196.execute(Unknown Source)
at org.apache.ibatis.executor.statement.PreparedStatementHandler.query(PreparedStatementHandler.java:63)
at org.apache.ibatis.executor.statement.RoutingStatementHandler.query(RoutingStatementHandler.java:79)
at org.apache.ibatis.executor.SimpleExecutor.doQuery(SimpleExecutor.java:63)
at org.apache.ibatis.executor.BaseExecutor.queryFromDatabase(BaseExecutor.java:324)
at org.apache.ibatis.executor.BaseExecutor.query(BaseExecutor.java:156)
at org.apache.ibatis.executor.CachingExecutor.query(CachingExecutor.java:109)
at com.github.pagehelper.PageInterceptor.intercept(PageInterceptor.java:112)
at org.apache.ibatis.plugin.Plugin.invoke(Plugin.java:61)
at com.sun.proxy.$Proxy194.query(Unknown Source)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:148)
at org.apache.ibatis.session.defaults.DefaultSqlSession.selectList(DefaultSqlSession.java:141)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43)
at java.lang.reflect.Method.invoke(Method.java:498)
at org.mybatis.spring.SqlSessionTemplate$SqlSessionInterceptor.invoke(SqlSessionTemplate.java:433)
... 78 common frames omitted
异常SQL语句:
SELECT
p.MemberId AS MemeberId,
p.ProjectFullName AS ProjectFullName,
CfmMoney AS changeMoney,
CfmTime AS updateTime,
1 AS bizType
FROM ${db_name_exchange}.bizmoney_cfmright_detail c
JOIN ${db_name_exchange}.bizorder_apply o ON(c.OrderApplyId=o.Id)
JOIN ${db_name_bizlisting}.listing_base p ON(c.ProjectId=p.Id)
WHERE o.OrderUserId=#{userId} AND c.CfmStatus IN(2,3)
UNION ALL
SELECT
p.ProjectFullName,
DrawBackMoney,
PayResultTime,
2
FROM ${db_name_exchange}.bizmoney_drawback_detail d
JOIN ${db_name_exchange}.bizmoney_cfmright_detail c ON(d.CfmRightDetailId=c.Id)
JOIN ${db_name_exchange}.bizorder_apply o ON(c.OrderApplyId=o.Id)
JOIN ${db_name_bizlisting}.listing_base p ON(d.ProjectId=p.Id)
WHERE o.OrderUserId=#{userId} AND d.PayStatus=2
UNION ALL
SELECT
p.ProjectFullName,
PayInvestMoney,
r.UpdateTime,
3
FROM ${db_name_exchange}.bizpayinvest_detail_result r J
OIN ${db_name_bizlisting}.listing_base p ON(r.ProjectId=p.Id)
WHERE InvestUserId=#{userId} AND r.PayInvestStatus=4
ORDER BY updateTime DESC;

错误原因:
由于自己加了一个p.MemberId AS MemeberId字段,造成了以上的异常报错信息,我们可以从The used SELECT statements have a different number of columns这句异常信息得出使用UNION关键字连接的三个SELECT语句查询出来的字段个数不一样,因此报错。

解决方案:
由于需求需要查询出来p.MemberId AS MemeberId字段,通过观察出原来的SQL语句可以看出三个SELECT语句查询出来的都有p.ProjectFullName AS ProjectFullName, p.ProjectFullName, p.ProjectFullName,,那么我们也可以按照需求这样做,修改后的SQL语句
SELECT
p.MemberId AS MemeberId,
p.ProjectFullName AS ProjectFullName,
CfmMoney AS changeMoney,
CfmTime AS updateTime,
1 AS bizType
FROM ${db_name_exchange}.bizmoney_cfmright_detail c
JOIN ${db_name_exchange}.bizorder_apply o ON(c.OrderApplyId=o.Id)
JOIN ${db_name_bizlisting}.listing_base p ON(c.ProjectId=p.Id)
WHERE o.OrderUserId=#{userId} AND c.CfmStatus IN(2,3)
UNION ALL
SELECT
p.MemberId,
p.ProjectFullName,
DrawBackMoney,
PayResultTime,
2
FROM ${db_name_exchange}.bizmoney_drawback_detail d
JOIN ${db_name_exchange}.bizmoney_cfmright_detail c ON(d.CfmRightDetailId=c.Id)
JOIN ${db_name_exchange}.bizorder_apply o ON(c.OrderApplyId=o.Id)
JOIN ${db_name_bizlisting}.listing_base p ON(d.ProjectId=p.Id)
WHERE o.OrderUserId=#{userId} AND d.PayStatus=2
UNION ALL
SELECT
p.MemberId,
p.ProjectFullName,
PayInvestMoney,
r.UpdateTime,
3
FROM ${db_name_exchange}.bizpayinvest_detail_result r J
OIN ${db_name_bizlisting}.listing_base p ON(r.ProjectId=p.Id)
WHERE InvestUserId=#{userId} AND r.PayInvestStatus=4

ORDER BY updateTime DESC;



阅读更多
想对作者说点什么? 我来说一句

没有更多推荐了,返回首页