2016-11-15 46 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'"; 

注:字符串,日期,日期時間,枚舉等必須在單引號。所以不要忘記用單引號綁定。