2011-12-17 84 views
-1

請參閱我的查詢,MySQL的插入子查詢不能

insert into wp_9m8phu_term_relationships as rel 
    (object_id,term_taxonomy_id,term_order) 
values 
    (
    'select object_id+1 as object_id from wp_9m8phu_term_relationships 
     where object_id!='' order by object_id desc limit 0,1', 
    '".$fnds_id."', 
    0 
) 

這查詢不工作。有人在這方面幫助我。

+0

你在哪裏,PHP調用它?什麼不工作,是否有錯誤信息? – codeling 2011-12-17 12:19:17

回答

2

您需要轉義單引號(')。您的查詢應該是這樣的:

insert into wp_9m8phu_term_relationships as rel (object_id,term_taxonomy_id,term_order) 
values('select object_id+1 as object_id from wp_9m8phu_term_relationships 
     where object_id!=\'\' order by object_id desc limit 0,1','".$fnds_id."',0 
) 

而且如果你從PHP調用該SQL語句(如第二個值建議),你應該確保你逃避包含的變量$fnds_id以及報價(如果使用mysqli來訪問數據庫,你應該使用mysqli::real_escape_string)。

0

我覺得有語法埃羅 試試這個

insert into wp_9m8phu_term_relationships as rel (object_id,term_taxonomy_id,term_order) 
values("select object_id+1 as object_id from wp_9m8phu_term_relationships 
     where object_id<>'' order by object_id desc limit 0,1",'".$fnds_id."',0)