如何使用mysql

时间:2016-10-13 11:12:44

标签: mysql sql

CREATE DEFINER=`root`@`localhost` PROCEDURE `EventList_SP`(
in employeeId varchar(45),
in eventTypevalue int,
in groupIdArray  text,
in searchTermtext text,
in skillIdArray text,
in startDate date, 
in endDate date,
in offsetvalue int,
out total int
 )
BEGIN

          SET @empID = employeeId;
          SET @Whereclause='where  1=1';

     if(groupIdArray is not null) then
    set @Whereclause=CONCAT(@Whereclause," and FIND_IN_SET(groupId,'",groupIdArray,"')" );
    end if;

      if(skillIdArray is not null) then
    set @Whereclause=CONCAT(@Whereclause," and FIND_IN_SET(groupId,'",skillIdArray,"')" );
    end if;
    if(startDate is not null and  endDate is not null) then
       set @Whereclause=CONCAT(@Whereclause," and (scheduledDate between
    '",startDate,"' and '",endDate,"')" );
     end if;
     if(eventTypevalue is not null and eventTypevalue=0) then
     set @Whereclause=CONCAT(@Whereclause," and scheduledDate<'",curdate(),"'" );
    end if;

      if(eventTypevalue is not null and eventTypevalue=1) then
            set @Whereclause=CONCAT(@Whereclause," and  scheduledDate>'",curdate(),"'" );
       end if;

       if(searchTermtext is not null ) then

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

set @SQLQuery  =CONCAT("select groupId,eventId,scheduleId,description,events,eventType,
scheduledDate,name,designation,image,skills,duration,status,
case when scheduledDate < now() and (select count(*) from event_request where 
event_id=eventId and employee_code='TJU_741')>0 then 1 
when scheduledDate < now() and (select count(*) from event_request where 
event_id=eventId and employee_code='TJU_741')=0 then 0 
else '' end as hasRequested ,(SELECT actual_attendance_status_id FROM TJU.event_attendees_mapping where 
 scheduleId=event_schedule_id and employee_code='TJU_741')
 as attendingStatus,
meetingRoom from EventList_View ", @Whereclause  );

 /* set @SQLQuery=CONCAT("select count(*) from EventList_View",@Whereclause,"into",total) ;
 */
  PREPARE stmt FROM @SQLQuery;
  EXECUTE stmt;
  DEALLOCATE PREPARE stmt;       

END

这是我的程序我能够在传递不同参数时获取数据我想获得总返回行数我必须设置为输出参数但是当我使用我的查询计数编号记录时我得到null评论请建议我如何使用动态查询获取记录总数

1 个答案:

答案 0 :(得分:1)

如果我理解正确,你可以这样设置:

set @sql = 'select SQL_CALC_FOUND_ROWS . . . ';
set @total = -1;
prepare @sql;
execute @sql;

execute 'select found_rows() into @total' ;

set out_total = @total;

注意:我将参数名称更改为out_total。您应该使用某种命名约定来区分参数名称和列名称。