如何在mySql过程中动态添加where子句

时间:2016-08-10 08:49:57

标签: mysql stored-procedures

where  

  b.deviceId=device_id_U 

  and( 
  CONCAT('%',COALESCE(device_id_U,''),'%') and
   b.searchTerm like
   CONCAT('%',COALESCE(search_term,''),'%') and  
   b.deviceName like
  CONCAT('%',COALESCE(device_name,''),'%') and
   b.deviceNumber    like
  CONCAT('%',COALESCE(device_Number,''),'%') and
  b.os like 
  CONCAT('%',COALESCE(device_os,''),'%') and
  b.deviceWorkingStatus like
  CONCAT('%',COALESCE(device_workingstatus_U,''),'%') and
  b.status like
  CONCAT('%',COALESCE(device_status_U,''),'%') )   

 ;
 end if;

END

这是我想要的程序,当我传递device_id null然后它应该忽略带有设备ID的条件,如果给出device_id值那么它应该搜索设备ID明智请告诉我如何动态添加条件以便我可以解决这个问题我面临着动态添加where子句的问题

1 个答案:

答案 0 :(得分:0)

你可以尝试反转@ FDavidov的想法

where  b.deviceId= IFNULL(device_id_U , b.deviceId)