使用select子句更新mysql作为更新值

时间:2014-04-18 02:32:30

标签: mysql sql sql-update phpbb3

我尝试将值从t2.f2复制到t1.f1,即PHPbb3论坛的主题和帖子表。我试过以下sql:

UPDATE t_topics SET
 topic_title = (SELECT t_posts.post_subject WHERE
  t_posts.post_id = t_topics.topic_first_post_id)
WHERE topic_id = 2;

我收到以下错误:

  MySQL说:

     

`#1064 - 你的SQL语法有错误;检查与MySQL服务器版本对应的手册,以获得正确的语法   'WHERE t_posts.post_id = t_topics.topic_first_post_id)附近   第1行的topic_id = 2'

我怎么能用sql实现这个?

1 个答案:

答案 0 :(得分:1)

您缺少from条款:

UPDATE t_topics
    SET topic_title = (SELECT t_posts.post_subject
                       FROM t_posts
                       WHERE t_posts.post_id = t_topics.topic_first_post_id
                      )
    WHERE topic_id = 2;