更新多个mysql表中的用户id列
我意识到这个问题似乎之前已经被问过并回答过,但我仍然不确定这是否可能。
我正在尝试更新 9 个表以反映 user_id 值的更改,有点像这样:
UPDATE table1, table2, table3, table4 SET
table1.id='12', table2.id='12', table3.id='12', table4.id='12'
WHERE table1.id='15' OR table2.id='15' OR table3.id='15' OR table4.id='15'
这可能吗?我是否错过了一些非常明显的事情?
I realise that this question seems to have been asked and answered before, but I'm still unsure of whether this is possible.
I'm trying to update 9 tables to reflect a change in user_id value, a bit like this:
UPDATE table1, table2, table3, table4 SET
table1.id='12', table2.id='12', table3.id='12', table4.id='12'
WHERE table1.id='15' OR table2.id='15' OR table3.id='15' OR table4.id='15'
Is that possible? Have I missed something really obvious?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
UPDATE的语法
似乎一次只能更新一张表。
The syntax of UPDATE is
it seems that you can only update one table at a time.
update 语句与 select 非常相似,所以您应该能够做到!
不过,我建议使用触发器。
update statement is very similar to select, so you should be able to do that!
However, i recommend using triggers.
将
OR
更改为AND
否则您将遇到问题。OR
将更新许多其他用户 IDchange
OR
toAND
Otherwise you will be in the problem.OR
will update many other user idsInnodb存储引擎提供对外键级联更新的支持 - http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
你应该考虑这个
或者
将单个查询分解为九个查询(每个表)以尽量减少犯错误的潜在风险(安全行事而不是聪明行事)
Innodb storage engine provide support on foreign key cascade update - http://dev.mysql.com/doc/refman/5.1/en/innodb-foreign-key-constraints.html
you should consider this
Alternatively
Break the single query to nine query (each for each table) to minimize potential risk on making mistake (play safe instead of play smart)