2017-06-05 264 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 
相關問題