как подсчитать общую запись с помощью динамического запроса в mysql

#mysql #sql

#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:

Если я правильно понимаю, вы бы настроили все следующим образом:

 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 . Вы должны отличать имена параметров от имен столбцов, используя какое-то соглашение об именовании.

Комментарии:

1. set @sql = ‘select SQL_CALC_FOUND_ROWS . . . ‘ что это за строка

2. @DevResearch . . . dev.mysql.com/doc/refman/5.7/en /.

3. где вы должны указать out_total в этой ошибке, выполните ‘select found_rows() в @total’ ;