2016-03-02 160 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

相關問題