使用子查询更新表

发布于 2024-11-27 01:55:55 字数 629 浏览 3 评论 0原文

正如你所看到的,我想要做的是:

[1] 获取与表 forum_qa 中的 $toggled 匹配的记录的 author_id

[2 ] 更新 user_profiles 中的 reputation,其中 user_idauthor_id 匹配,

    UPDATE  user_profiles

    (SELECT forum_qa_author_id AS author_id
    FROM    forum_qa
    WHERE   forum_qa_id = $toggled) AS f

    SET     user_profiles.reputation = user_profiles.reputation - 15
    WHERE   user_profiles.user_id = f.author_id

这给了我一个 1064 语法错误(SELECT...

知道我在这里做错了什么吗?

感谢您的帮助!

As you can see what I want to do is:

[1] grab the author_id of a record that matches $toggled in table forum_qa

[2] update reputation in user_profiles where user_id matches author_id

    UPDATE  user_profiles

    (SELECT forum_qa_author_id AS author_id
    FROM    forum_qa
    WHERE   forum_qa_id = $toggled) AS f

    SET     user_profiles.reputation = user_profiles.reputation - 15
    WHERE   user_profiles.user_id = f.author_id

This is giving me a 1064 syntax error at (SELECT....

Any idea what I'm doing wrong here?

Thanks for helping!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

慢慢从新开始 2024-12-04 01:55:55

尝试:

UPDATE  user_profiles
SET     user_profiles.reputation = user_profiles.reputation - 15
WHERE   user_profiles.user_id = (SELECT forum_qa_author_id AS author_id
FROM    forum_qa
WHERE   forum_qa_id = $toggled)

Try:

UPDATE  user_profiles
SET     user_profiles.reputation = user_profiles.reputation - 15
WHERE   user_profiles.user_id = (SELECT forum_qa_author_id AS author_id
FROM    forum_qa
WHERE   forum_qa_id = $toggled)
无敌元气妹 2024-12-04 01:55:55
UDPATE up set reputation = reputation - 15 
FROM user_profiles up inner join forum_qa fq on (up.user_id = fq.forum_qa_id) 
WHERE forum_qa_id = $toggled

我刚刚意识到你正在使用MySQL。这是针对 MS SQL 的,我不确定你是否可以在 MySQL 中以完全相同的方式使用它,但希望它能有一点帮助。

UDPATE up set reputation = reputation - 15 
FROM user_profiles up inner join forum_qa fq on (up.user_id = fq.forum_qa_id) 
WHERE forum_qa_id = $toggled

I just realised that you are using MySQL. This is for MS SQL, I'm not sure if you can use it exactly the same way in MySQL but hopefully it's a little bit of help..

月下伊人醉 2024-12-04 01:55:55

子查询必须位于“SET”之后。在这种情况下,它可能不一定需要子查询...

尝试...

UPDATE user_profiles
   SET user_profiles.reputation = user_profiles.reputation - 15
  FROM user_profiles
  JOIN forum_qa f ON user_profiles.user_id = f.author_id
   AND forum_qa_id = $toggled

The subquery has to come after the "SET" . In this case it might not necessarily need a sub query...

try...

UPDATE user_profiles
   SET user_profiles.reputation = user_profiles.reputation - 15
  FROM user_profiles
  JOIN forum_qa f ON user_profiles.user_id = f.author_id
   AND forum_qa_id = $toggled
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文