如何在mysql中连接数据并进行动态查询

时间:2016-11-21 07:45:44

标签: mysql sql

CREATE DEFINER=`root`@`localhost` PROCEDURE `GetMyEvent_SP`(in employeeId varchar(45),
in eventType int,

in searchTermtext text,
in skillIdArray text,
in startDate varchar(50),
in endDate varchar(50),
in offsetvalue int,

in recordlimit int,
out total int
 )
BEGIN

declare totalcount int default 0;
          SET @Whereclause='where  1=1 and isMyEvent=1';
          SET @empID = concat('"',employeeId,'"');
          SET @SearchTerm=concat('"%',searchTermtext,'%"');
          SET @startdate=concat('"',startDate,'"');
          SET @enddate=concat('"',endDate,'"');
          SET @currentdate=concat('"',curdate(),'"');

          SET @skillid=concat('"',skillIdArray,'"');


    if(skillIdArray is not null and skillIdArray <> '') then

      set @Whereclause=CONCAT(@Whereclause," and FIND_IN_SET(skills,",@skillid,")" );
    end if;

   if(eventType is not null and eventType=0 and eventType<>-1 ) then
     set @Whereclause=CONCAT(@Whereclause," and date(scheduledDate)<",@currentdate );
      end if;

      if(eventType is not null and eventType=1) then
            set @Whereclause=CONCAT(@Whereclause," and  date(scheduledDate)>",@currentdate);
       end if;

       if(searchTermtext is not null and  searchTermtext<>'') then

    set @Whereclause=CONCAT(@Whereclause," and  searchTerm like ",@SearchTerm );
    end if;


  if(startDate is not null and endDate is not null and startDate<>'1111/11/11' and endDate<>'1111/11/11'  ) then
       set @Whereclause=CONCAT(@Whereclause," and (date(scheduledDate) between ",@startdate," and ",@enddate,")" );
     end if;


set @SQLQuery  =CONCAT("select eventId,scheduleId,description,events,eventType,
scheduledDate,name,designation,image,skills,duration,status,attachmentPath,
attendingStatus,meetingRoom,hasRequested from EventList_View ", @Whereclause);

 set @CountSQLQuery=concat("select count(*) from EventList_View ", @Whereclause," into ",total);



 if(offsetvalue<recordlimit) then
    set @QueryfforPagination=CONCAT(@SQLQuery," limit ",recordlimit );
     PREPARE querystatement FROM @QueryfforPagination;
   EXECUTE querystatement;
   DEALLOCATE PREPARE querystatement;

    else
     set  @QueryfforPagination=CONCAT(@SQLQuery," limit ",recordlimit," offset ",offsetvalue);

       PREPARE querystatement FROM @QueryfforPagination;
   EXECUTE querystatement;
   DEALLOCATE PREPARE querystatement;

    end if;

   select @CountSQLQuery;


END 

这是我的查询我能够获取数据我必须计算总记录数我已经进行了一次查询

set @CountSQLQuery=concat("select count(*) from EventList_View ", @Whereclause," into ",total);

但是当我尝试在@CountSQLQuery中连接时,它总是给null它不会连接请告诉我在哪里做错了为什么@CountSQLQuery不能连接以便我可以得到数据请建议我。

0 个答案:

没有答案