Search code examples
phpmysqlsql-updatesql-like

Like is not working as expected in update query of join condition


Code :

UPDATE elections_ls_2014_stats e
      JOIN `pc-details` p
      ON ( e.name LIKE CONCAT('%' ,p.candidate_name ,'%') )
SET    e.pc_details_id = p.id  WHERE  p.year =  '2014'
AND p.`candidate_results_position` ='1' AND e.pc_details_id IS NULL

Here its not working as expected eg :Dr Pandula Ravindra Babu AND Pandula Ravindra Babu

this is one of the case where this condition is not working as expected , means update query is not working .

but in select statement I am getting the result for the above cases .(Dr Pandula Ravindra Babu AND Pandula Ravindra Babu)

code :

SELECT pd.id AS pc_details_id, el.id AS election_2014_id, pd.candidate_name, el.name
FROM  `pc-details` pd,  `elections_ls_2014_stats` el
WHERE pd.`year` LIKE  '2014'
AND pd.`candidate_results_position` =1
AND pd.candidate_name LIKE CONCAT(  '%', el.name,  '%' ) 

Am I doing anything wrong in the update query .

Any suggestion will be appreciated.Thank u


Solution

  • In your UPDATE query the LIKE expression is the opposite way around to the way it is in your SELECT query. Try this instead:

    UPDATE elections_ls_2014_stats e
          JOIN `pc-details` p
          ON p.candidate_name LIKE CONCAT('%', e.name,  '%') 
    SET    e.pc_details_id = p.id  WHERE  p.year =  '2014'
    AND p.`candidate_results_position` ='1' AND e.pc_details_id IS NULL