2016-11-15 181 views
0
CREATE DEFINER=`root`@`localhost` PROCEDURE `test2`(in employeeId text) 
BEGIN 
set @SQLQuery =CONCAT("select groupId,eventId,scheduleId,description,events,eventType, 
scheduledDate,name,designation,image,skills,duration,status,attachmentPath, 
case when scheduledDate < now() and (select count(*) from event_request where 
event_id=eventId and employee_code='",employeeId,"')>0 then 1 
when scheduledDate < now() and (select count(*) from event_request where 
event_id=eventId and employee_code='",employeeId,"')=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='",employeeId,"') 
as attendingStatus, 
case 
when 
(select count(*) from event_attendees_mapping where 
event_schedule_id=scheduleId and employee_code='",employeeId,"')>0 then 1 
else 0 end as isMyEvent,meetingRoom from EventList_View ", "and 1=1"); 

select @SQLQuery; 

END 

这是我的动态查询过程,我传递动态员工ID,当我调用此过程时(“TJU_741”);如何通过mysql中的select查询动态ID

然后我的查询变得

select groupId,eventId,scheduleId,description,events,eventType, 
scheduledDate,name,designation,image,skills,duration,status,attachmentPath, 
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, 
case 
when 
(select count(*) from event_attendees_mapping where 
event_schedule_id=scheduleId and employee_code=''TJU_741'')>0 then 1 
else 0 end as isMyEvent,meetingRoom from EventList_View and 1=1 

在这里你可以看到每个雇员变成这样'TJU_741“”,而应该“TJU_741”请建议我如何Concat的,这样我的查询成为员工ID 'TJU_741'。

+0

看起来'employeeId'列已经被单引号转义了,所以也许你应该删除连接字符串中的单引号? –

回答

1

您的查询必须像:

$param1 = 10; 
$param2 = 'Max'; 

$query = "select column from table where column = ".$param1." and "'.$param2.'" and column = '1'"; 

注:字符串,日期,日期时间,枚举等必须在单引号。所以不要忘记用单引号绑定。