我正在尝试从 comment_users 表 user_id 列值更新 comments 表上的 user_id 列,其中 comment_id 与 comments 表 id 列匹配。
评论用户表
id: 5 comment_id: 1, user_id: 20
Run Code Online (Sandbox Code Playgroud)
之前的评论表
id:1 user_id: NULL
Run Code Online (Sandbox Code Playgroud)
后
id: 1 user_id: 20
Run Code Online (Sandbox Code Playgroud)
我执行了下面的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
Run Code Online (Sandbox Code Playgroud)
我不知道出了什么问题。
您的update join语法错误,请尝试以下操作:
UPDATE comments
INNER JOIN comment_users ON comment_users.comment_id = comments.id
SET comments.user_id = comment_users.user_id
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
10128 次 |
| 最近记录: |