2011-07-29 99 views
0

正如你可以看到我想要做更新的表是:使用子查詢

[1]搶AUTHOR_ID在表匹配$toggled記錄的forum_qa

[2]在user_profiles其中user_id說明比賽更新聲譽 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 

這給我一個1064語法錯誤在(SELECT...

任何想法我在這裏做錯了嗎?

感謝您的幫助!

回答

4

嘗試:

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) 
0

子查詢有來的 「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 
0
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中以完全相同的方式使用它,但希望它有一點幫助。