我試圖更新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
我不知道什麼是錯的。
該表是您在SET命令的user_id是從哪裏來的? –
試試這個,UPDATE註釋INNER JOIN comment_users ON comment_users.comment_id = comments.id SET user_id = comment_users.user_id –
@FahadAnjum它不起作用,因爲SET列後面的user_id含糊不清,但它可以改變comments.user_id!感謝您的幫助 – DIGITALSQUAD