mysql更新查询,合并两个值
我有一个像这样的表
USER | DATA
----------------
User1 | 123
User1 | 456
User2 | 456
User3 | 123
User4 | 789
,并且我对用户数据有一个唯一约束。现在我想用“123”替换所有“456”,所以最后我
USER | DATA
----------------
User1 | 123
User2 | 123
User3 | 123
User4 | 789
真的认为这很容易,u_U知道如何继续吗?任何帮助将不胜感激=)
谢谢
I have a table like this
USER | DATA
----------------
User1 | 123
User1 | 456
User2 | 456
User3 | 123
User4 | 789
and i have a UNIQUE constraint for User-Data. Now i want to replace all "456" with "123", so in the end i'd have
USER | DATA
----------------
User1 | 123
User2 | 123
User3 | 123
User4 | 789
I really thought that it would be easy, u_U any idea how to proceed? any help would be appreciated =)
Thxs
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
“使用 IGNORE 关键字,即使更新期间发生错误,更新语句也不会中止。发生重复键冲突的行不会更新”
更新不会违反唯一约束的任何行。
如果有任何行,只需删除它们
"With the IGNORE keyword, the update statement does not abort even if errors occur during the update. Rows for which duplicate-key conflicts occur are not updated"
So,
Update any rows that would not violate the unique constraint.
If any rows would, just delete them
并不是那么难。
但是您想如何处理这种情况:
?
Not so hard was it.
But how do you want to deal with this scenario:
?
如果您有唯一约束,则不能有 User1 - 123 的两个条目。因此您的最后结果是正确的。您也不能拥有 User2 - 456 的两个条目。因为您有一个与两个字段相结合的唯一约束。
If you have a unique constraint, you can't have two entries with User1 - 123. So your last result is correct. You also can't have two entries with User2 - 456. Cause you have a unique constraint combined with both fields.