如果有多个字段,则重复密钥更新

时间:2014-03-05 14:13:32

标签: php mysql

INSERT INTO highscore(user, points, modality, time, level)
VALUES('$user', '$score', '$modality', '$time', '$level')

ON DUPLICATE KEY UPDATE
  points = IF(VALUES(points) > points, VALUES(points), points),
  time = IF(VALUES(points) > points, VALUES(time), time),
  level = IF(VALUES(points) > points, VALUES(level), level)

UNIQUE字段是“user”和“modality”

如果old_points为>此代码不起作用。 new_points此查询仅更新points字段。当新点> 1时,我必须使用相应的值更新这3个字段。比旧的。

如何解决?

2 个答案:

答案 0 :(得分:1)

使用此

points = IF(VALUES(points) > points, VALUES(points), points),
time = IF(VALUES(points) >= points, VALUES(time), time),
level = IF(VALUES(points) >= points, VALUES(level), level)

注意>=符号

说明:在第一行中,points的值已经更改

答案 1 :(得分:1)

以下只是一个证明,您的代码很好,完全按照您的期望行事。

mysql> select version();
+------------------------+
| version()              |
+------------------------+
| 5.1.41-3ubuntu12.7-log |
+------------------------+
1 row in set (0.00 sec)

 create table highscore(user int, points int, modality int, time int, level int, unique key idx_u_m (use                         r, modality));
Query OK, 0 rows affected (0.11 sec)

mysql> INSERT INTO highscore(user, points, modality, time, level)
    -> VALUES(1, 1, 1, 1, 1)
    -> ON DUPLICATE KEY UPDATE
    -> points = IF(VALUES(points) > points, VALUES(points), points),
    -> time = IF(VALUES(points) > points, VALUES(time), time),
    -> level = IF(VALUES(points) > points, VALUES(level), level);
Query OK, 1 row affected (0.01 sec)

mysql> INSERT INTO highscore(user, points, modality, time, level)
    -> VALUES(1, 1, 1, 1, 1)
    -> ON DUPLICATE KEY UPDATE
    -> points = IF(VALUES(points) > points, VALUES(points), points),
    -> time = IF(VALUES(points) > points, VALUES(time), time),
    -> level = IF(VALUES(points) > points, VALUES(level), level);
Query OK, 0 rows affected (0.15 sec)

mysql> select * from highscore;
+------+--------+----------+------+-------+
| user | points | modality | time | level |
+------+--------+----------+------+-------+
|    1 |      1 |        1 |    1 |     1 |
+------+--------+----------+------+-------+
1 row in set (0.00 sec)

mysql> INSERT INTO highscore(user, points, modality, time, level)
    -> VALUES(1, 2, 1, 1, 1)
    -> ON DUPLICATE KEY UPDATE
    -> points = IF(VALUES(points) > points, VALUES(points), points),
    -> time = IF(VALUES(points) > points, VALUES(time), time),
    -> level = IF(VALUES(points) > points, VALUES(level), level);
Query OK, 2 rows affected (0.09 sec)

mysql> select * from highscore;                                                                                                        +------+--------+----------+------+-------+
| user | points | modality | time | level |
+------+--------+----------+------+-------+
|    1 |      2 |        1 |    1 |     1 |
+------+--------+----------+------+-------+
1 row in set (0.00 sec)

mysql> INSERT INTO highscore(user, points, modality, time, level)
    -> VALUES(2, 2, 1, 1, 1)
    -> ON DUPLICATE KEY UPDATE
    -> points = IF(VALUES(points) > points, VALUES(points), points),
    -> time = IF(VALUES(points) > points, VALUES(time), time),
    -> level = IF(VALUES(points) > points, VALUES(level), level);
Query OK, 1 row affected (0.03 sec)

mysql> select * from highscore;
+------+--------+----------+------+-------+
| user | points | modality | time | level |
+------+--------+----------+------+-------+
|    1 |      2 |        1 |    1 |     1 |
|    2 |      2 |        1 |    1 |     1 |
+------+--------+----------+------+-------+
2 rows in set (0.00 sec)

mysql> INSERT INTO highscore(user, points, modality, time, level)
    -> VALUES(2, 3, 1, 1, 1)
    -> ON DUPLICATE KEY UPDATE
    -> points = IF(VALUES(points) > points, VALUES(points), points),
    -> time = IF(VALUES(points) > points, VALUES(time), time),
    -> level = IF(VALUES(points) > points, VALUES(level), level);
Query OK, 2 rows affected (0.08 sec)

mysql> select * from highscore;
+------+--------+----------+------+-------+
| user | points | modality | time | level |
+------+--------+----------+------+-------+
|    1 |      2 |        1 |    1 |     1 |
|    2 |      3 |        1 |    1 |     1 |
+------+--------+----------+------+-------+
2 rows in set (0.00 sec)