2016-03-02 208 views
0

我有两个表:rec_new_license和recruit_zips。如果值从另一个表中选择,则更新表

recruit_zips包含'zip_code'和'office_name',包含邮政编码和办公室名称。

rec_new_license包含一个字段'zip'和一个字段'recruit_office'。 如果'zip'中的值与recruit_zips表中的'zip_code'相匹配,并且'office_name'与'Spring Hill'匹配,则此表需要更新'recruit_office'字段。

这两个查询都不执行任务或失败并显示错误。我是否在谈论这个错误?

$sql = "UPDATE rec_new_license 
SET recruit_office = 'Spring Hill' 
WHERE zip IN 
( 
SELECT zip_code FROM recruit_zips 
WHERE office_name = 'Spring Hill' 
)"; 

$results = $mysqli->query($sql); 

也试过:

$sql = "UPDATE rec_new_license t1 
    JOIN recruit_zips t2 
    ON t1.zip = t2.zip_code 
    WHERE t2.office_name = 'Spring Hill' 
SET t1.recruit_office = 'Spring Hill' 
"; 

回答

0

你的语法是错误的。它必须是:

$sql = "UPDATE rec_new_license t1 
    JOIN recruit_zips t2 
    ON t1.zip = t2.zip_code 
    SET t1.recruit_office = 'Spring Hill' 
    WHERE t2.office_name = 'Spring Hill' 
"; 
+0

工作。还需要添加: ON t1.zip COLLATE utf8_unicode_ci = t2.zip_code 谢谢! – user2413654

相关问题