开发者

Updating a table using a subquery

开发者 https://www.devze.com 2023-03-24 02:34 出处:网络
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

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    开发者_JAVA百科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!


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)


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..


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
0

精彩评论

暂无评论...
验证码 换一张
取 消