0

当多个唯一键时是否可以有一个或选项?

例如我要检查这个键或这个键,如果一个人踢出一个重复的错误然后更新那个键?

我有 2 个唯一键:UPN 和(用户名,学校)

我们在 out MYSQL 中使用了 on duplicate update 语句。如果通过了 UPN,则在重复或插入新行时更新 upn 键。我想检查 UPN 和(用户名、学校)并执行以下操作:

If upn match update on that if (username,school) match update on that if upn dosnt match but (username,school) do update on (username,school) match If upn match and (username,school) dosnt update on UPN match

所以我需要它来使用 or 代替 and,这可能吗?

4

1 回答 1

1

ON DUPLICATE您可以在查询的子句中使用条件运算符来模拟这一点:

INSERT INTO thetable (id, username, school, othervalue)
VALUES (10, 'newtover', 'cool school', 'a comment')
ON DUPLICATE KEY UPDATE
  username = IF(id = VALUES(id), VALUES(username), username),
  school = IF(id = VALUES(id), VALUES(school), school),
  id = IF(id = VALUES(id), id, VALUES(id));

在这种情况下,更新的顺序很重要。

于 2011-12-12T14:24:14.347 回答