简体   繁体   中英

UPDATE SET CASE with an INNER JOIN?

I'm trying to update a field in one of our relational tables. The table in question is a user-skill relationship table. The field I'm trying to update is the proficiency field.

I have an array in PHP with the name of the skill as the key and the proficiency as the value.

Here's the SQL Statement I'm running.

UPDATE rus 
SET proficiency = CASE 
    WHEN ls.name = 'Objective-C' THEN 'Beginner' 
    END 
FROM rel_users_li_skills rus INNER JOIN li_skills ls ON ls.id = rus.skill_id 
WHERE rus.user_id = 3852

The PHP array is enumerated through to add extra WHEN ls.name = 'skill-name' THEN 'proficiency' statements in the CASE statement.

The problem I'm having is the following :

check the manual that corresponds to your MySQL server version for the right syntax to use near 'FROM rel_users_li_skills rus INNER JOIN li_skills ls ON ls.id = rus.skill_id WHE' at line 1

I'm stumped on this one. I've found a couple of SO posts and forums already that seem to use the same syntax and have no problems.

In MySQL , unlike SQL Server , you don't need to refer the target table in FROM :

UPDATE  rel_users_li_skills rus
JOIN    li_skills ls
ON      ls.id = rus.skill_id 
SET     proficiency = CASE WHEN ls.name = 'Objective-C' THEN 'Beginner' END 
WHERE   rus.user_id = 3852

I usually used this kind of format.

UPDATE  rel_users_li_skills rus INNER JOIN li_skills ls
           ON ls.id = rus.skill_id 
   SET  proficiency = (CASE WHEN ls.name = 'Objective-C' THEN 'Beginner' END)
 WHERE  rus.user_id = 3852

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM