使用union进行多表查询

我在这边写了个存储过程,

--procedure_pdxx存储过程用于查询还未处理的排队信息,统计各个业务未处理的数量
IF EXISTS(SELECT * FROM sysobjects WHERE OBJECT_ID('procedure_pdxx') = ID AND type = 'P')
    DROP PROCEDURE procedure_pdxx
GO
PRINT 'CREATE PROCEDURE procedure_pdxx ......'
GO

create procedure procedure_pdxx
as
begin
	select COUNT(ZYBH) as count,'txxxbCount' as name from TXXXB where TXKSSJ is null and TXJSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0
	union
	select COUNT(ZYBH) as 'count','tjcsbCount' as name from TJCSDJB where CSSJ is null and HSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0
	union
	select COUNT(ZYBH) as 'count','lshjCount' as name from LSHJXXB where HJKSSJ is null and HJJSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0
	union
	select COUNT(ZYBH) as 'count','jshjCount' as name from JSHJXXB where HJKSSJ is null and HJJSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0;
end

exec procedure_pdxx;

exec procedure_pdxx得到的结果是

使用union进行多表查询_第1张图片

我是用mybatis将这个结果返回出去,这个返回的结果是list集合了


  

现在我思考一个问题,怎么样返回一个map集合呢

我还是写一个存储过程,如下:

--procedure_pdxx存储过程用于查询还未处理的排队信息,统计各个业务未处理的数量
IF EXISTS(SELECT * FROM sysobjects WHERE OBJECT_ID('procedure_pdxx') = ID AND type = 'P')
    DROP PROCEDURE procedure_pdxx
GO
PRINT 'CREATE PROCEDURE procedure_pdxx ......'
GO

create procedure procedure_pdxx
as
begin
	declare @txxxbCount int,@tjcsbCount int,@lshjCount int,@jshjCount int;
	
	select @txxxbCount = count(ZYBH) from TXXXB where TXKSSJ is null and TXJSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0
	
	select @tjcsbCount = count(ZYBH) from TJCSDJB where CSSJ is null and HSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0
	
	select @lshjCount = count(ZYBH) from LSHJXXB where HJKSSJ is null and HJJSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0
	
	select @jshjCount = COUNT(ZYBH) from JSHJXXB where HJKSSJ is null and HJJSSJ is null and DATEDIFF(dd,getDate(),DJRQ)=0;

	select @txxxbCount as txxxbCount,@tjcsbCount as tjcsbCount,@lshjCount as lshjCount,@jshjCount as jshjCount;
end

exec procedure_pdxx;

exec procedure_pdxx的结果是:

txxxbCount tjcsbCount lshjCount jshjCount
0 0 0 0

然后mybatis中mapper的写法如下:


  
Map selectPdxxCount();

 

你可能感兴趣的:(java,数据库,mybatis)