mysql How to update multiple columns with different data?

atmip9wb  于 2022-12-22  发布在  Mysql
关注(0)|答案(1)|浏览(103)

I want to change data from different columns with different values. Should I use loop in nodejs and run the query for each data? or there is another query which can fulfill this requiremnt. I have users table. Now i have to change the name of two users. In the given code i want to change the name of user with id=1 to name1 and the user and the user with id=2 to name2 . The following query generating error. What is the right way and syntax to do such a thing.

UPDATE
  UsersTable
SET
  `names` =('name1', 'name2')
WHERE
  id IN(1, 2)
wz1wpwve

wz1wpwve1#

UPDATE UsersTable
JOIN ( SELECT 'name1' names, 1 id 
       UNION ALL
       SELECT 'name1', 2 ) data USING (id)
SET UsersTable.names = data.names;

相关问题