如何在mysql中使用select查询来识别动态ID

时间:2016-11-15 04:40:29

标签: mysql sql

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”);

然后我的查询变为

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

在这里你可以看到每个employeeID都变成了这个''TJU_741',而它应该'TJU_741'请建议我如何连接,以便我的查询成为员工ID'TJU_741'。

1 个答案:

答案 0 :(得分:1)

您的查询必须如下:

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

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

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