Can I use SQL to update multiple rows with multiple values from another table?

Posted by stu on Stack Overflow See other posts from Stack Overflow or by stu
Published on 2010-03-08T21:50:39Z Indexed on 2010/03/08 21:51 UTC
Read the original article Hit count: 252

Filed under:
|

I've got a one to many table with ids and traits for those users. I want to copy one user's settings to another.

userid    trait    onoff
------    -----    -----
1          1         on
1          2         on
1          3         on
1          4         on
2          1         off
2          2         off
2          3         off
2          4         off

So I want to copy the onoff values from user 1 to 2, so that user2 has everything on when I'm done.

update table set onoff = (select onoff from table where userid=1) where userid=2

that's the basic idea, but obviously doesn't work. I imagine I want the server to think in terms of a correlated subquery, but the outer part isn't a query, it's an update.

Is this even possible in one step?

© Stack Overflow or respective owner

Related posts about sql

Related posts about tips-and-tricks