我试图更新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