从select更新MySQL

时间:2017-03-31 15:05:43

标签: php mysql

遇到此查询时遇到问题:

UPDATE table1 t1 SET t1.custom_id=(SELECT t2.custom_id 
FROM table2 t2 WHERE LOWER(t1.first_name)=LOWER(t2.first_name) 
AND LOWER(t1.last_name)=LOWER(t2.last_name) 
AND (t2.zip1 LIKE CONCAT('%',t1.zip,'%') 
OR t2.zip2 LIKE CONCAT('%',t1.zip,'%')) LIMIT 1)

我三重检查了php变量是否与字段名称匹配。当我将它分成多个查询时,更新工作正常。

1 个答案:

答案 0 :(得分:0)

您可能首先尝试搜索StackOIverflow以获得答案,但在您的情况下,这样的事情应该有效:

 UPDATE table1 t1
 join table2 t2 on
 LOWER (t1.first_name) = LOWER (t2.first_name)
        AND LOWER (t1.last_name) = LOWER (t2.last_name)
         AND (   t2.zip1 LIKE CONCAT ('%', t1.zip, '%')
             OR t2.zip2 LIKE CONCAT ('%', t1.zip, '%')) LIMIT 1)
 SET custom_id = t2.custom_id
相关问题