2017-06-05 118 views
0

我试图更新comment_users表中的user_id列user_id列值,其中comment_id与评论表id列匹配。从另一个表列值更新列

comment_users表

id: 5 comment_id: 1, user_id: 20 

意见表

id:1 user_id: NULL 

id: 1 user_id: 20 

我在下面执行了SQL,但它不起作用。

UPDATE comments 
    SET user_id = comment_users.user_id 
    INNER JOIN comment_users ON comment_users.comment_id = comments.id 
    WHERE comment_users.comment_id = comments.id 


Mysql2::Error: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'INNER JOIN comment_users ON comment_users.comment_id = comment' at line 3:  UPDATE comments 
     SET user_id = comment_users.user_id 
     INNER JOIN comment_users ON comment_users.comment_id = comments.id 
     WHERE comment_users.comment_id = comments.id 

我不知道什么是错的。

+0

该表是您在SET命令的user_id是从哪里来的? –

+0

试试这个,UPDATE注释INNER JOIN comment_users ON comment_users.comment_id = comments.id SET user_id = comment_users.user_id –

+0

@FahadAnjum它不起作用,因为SET列后面的user_id含糊不清,但它可以改变comments.user_id!感谢您的帮助 – DIGITALSQUAD

回答

1

update join语法是错误的,尝试以下操作:

UPDATE comments 
INNER JOIN comment_users ON comment_users.comment_id = comments.id 
SET comments.user_id = comment_users.user_id 
+0

这完美的作品!感谢您的回答。 – DIGITALSQUAD

0

你缺少评论的表名称?

UPDATE comments 
SET user_id = comment_users.user_id 
FROM [table_name] comments 
INNER JOIN comment_users ON comment_users.comment_id = comments.id 
WHERE comment_users.comment_id = comments.id 
相关问题