我试图了解如何更新具有不同值的多行,但我只是不明白。解决方案无处不在,但对我来说似乎很难理解。
例如,三个更新为 1 个查询:
UPDATE table_users SET cod_user = '622057' , date = '12082014' WHERE user_rol = 'student' AND cod_office = '17389551'; UPDATE table_users SET cod_user = '2913659' , date = '12082014' WHERE user_rol = 'assistant' AND cod_office = '17389551'; UPDATE table_users SET cod_user = '6160230' , date = '12082014' WHERE user_rol = 'admin' AND cod_office = '17389551';
我读了一个例子,但我真的不明白如何进行查询。IE:
UPDATE table_to_update SET cod_user= IF(cod_office = '17389551','622057','2913659','6160230') ,date = IF(cod_office = '17389551','12082014') WHERE ?? IN (??) ;
如果 WHERE 和 IF 条件中有多个条件,我不完全清楚如何进行查询..有什么想法吗?
你可以这样做:
UPDATE table_users SET cod_user = (case when user_role = 'student' then '622057' when user_role = 'assistant' then '2913659' when user_role = 'admin' then '6160230' end), date = '12082014' WHERE user_role in ('student', 'assistant', 'admin') AND cod_office = '17389551';
我不明白你的日期格式。日期应使用本地日期和时间类型存储在数据库中。