mysql插入子查询不起作用

时间:2011-12-17 11:58:43

标签: php 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
)

这是查询无效。有人在这方面帮助我。

2 个答案:

答案 0 :(得分: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)。

答案 1 :(得分: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)