mysql更新查询,合并两个值

发布于 2024-11-14 13:14:57 字数 371 浏览 7 评论 0原文

我有一个像这样的表

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

指尖凝香 2024-11-21 13:14:57

“使用 IGNORE 关键字,即使更新期间发生错误,更新语句也不会中止。发生重复键冲突的行不会更新”

更新不会违反唯一约束的任何行。
如果有任何行,只需删除它们

UPDATE IGNORE Table SET data=123 WHERE data=456 
DELETE FROM Table WHERE data=456  

"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

UPDATE IGNORE Table SET data=123 WHERE data=456 
DELETE FROM Table WHERE data=456  
迷迭香的记忆 2024-11-21 13:14:57
UPDATE IGNORE yourtable SET `data`=123 WHERE `data`=456

并不是那么难。

但是您想如何处理这种情况:

USER   |  DATA
--------------
userx  |  123
userx  |  456

UPDATE IGNORE yourtable SET `data`=123 WHERE `data`=456

Not so hard was it.

But how do you want to deal with this scenario:

USER   |  DATA
--------------
userx  |  123
userx  |  456

?

巨坚强 2024-11-21 13:14:57

如果您有唯一约束,则不能有 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文