2016-07-01 48 views
0

我正在嘗試使用MySql Quariy更改帖子並不斷收到1093 Err 我嘗試了所有有關使用子查詢的提示沒有成功err#1093 - 您無法指定目標表'wp_posts'在FROM子句中進行更新

也許有人可以幫我

UPDATE wp_posts SET post_content = CONCAT(post_content , 'testext_bla_bla') 
WHERE wp_posts.id in (select wp_posts.ID 
from wp_posts 
LEFT JOIN wp_term_relationships on wp_posts.ID = wp_term_relationships.object_id 
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id 
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id 
WHERE wp_posts.post_excerpt like '%LO1185LH%' and wp_term_taxonomy.term_taxonomy_id = 27) 

,並不斷收到「#1093 - 你不能指定更新目標表‘wp_posts’FROM子句」

感謝 阿薩夫

回答

0

使用alias爲您更新的表;)

UPDATE wp_posts t1 SET t1.post_content = CONCAT(t1.post_content, 'testext_bla_bla') 
WHERE t1.id IN (
SELECT wp_posts.ID 
FROM wp_posts 
LEFT JOIN wp_term_relationships ON wp_posts.ID = wp_term_relationships.object_id 
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id 
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id 
WHERE wp_posts.post_excerpt LIKE '%LO1185LH%' AND wp_term_taxonomy.term_taxonomy_id = 27) 

或者你可以只使用LEFT JOIN做到這一點;)

UPDATE wp_posts t1 
LEFT JOIN wp_term_relationships ON t1.ID = wp_term_relationships.object_id 
LEFT JOIN wp_term_taxonomy ON wp_term_taxonomy.term_taxonomy_id = wp_term_relationships.term_taxonomy_id 
LEFT JOIN wp_terms ON wp_terms.term_id = wp_term_relationships.term_taxonomy_id 
SET t1.post_content = CONCAT(t1.post_content, 'testext_bla_bla') 
WHERE t1.post_excerpt LIKE '%LO1185LH%' AND wp_term_taxonomy.term_taxonomy_id = 27 
0

這是因爲更新語句不從,因爲你設置表後更新UPDATE子句,如:

UPDATE wp_posts 
SET column1=value, column2=value2,... 
WHERE some_column = some_value. 

數據庫引擎是taki納克FROM子句,因爲它的UPDATE句子裏面(因爲它是一個更新語句)來處理它是最容易的方式選擇第一個(和PHP),並用這個結果來處理它的更新,如:

$result = connection()->query("SELECT blah_blah_blah"); 
connection()->close(); 
$rs = mysqli_fetch_array($result); 
//ok you got the result on $rs, if you wanna do an update with this it should 
//be unique result, if you want to do it recursively, simply act over $rs[$i]; 
//then, once or in a loop: 
$sql = "UPDATE wp_posts 
SET column1=value, column2=value2,... 
WHERE some_column = ".$rs['index_that_you_want'].";"; 
connection()->query($sql); 

(記得使用$ rs = mysqli_fetch_array($ result)取下一行;在循環的底部,如果你需要遞歸運行它。

相關問題