两个记录如何将单元格的值增加1

时间:2015-02-19 23:25:55

标签: mysql

我正在尝试编写一个MySQL查询,将两个记录的点(一个属性)增加1,其中一个用于x_id = 1和age = 57的记录,第二个用于y_id = 13和age = 36的记录,I我想做这样的事情:

update myTable set points = points+1
where (x_id,age)=(1,57) and (y_id,age)=(13,36);

但这不起作用请帮助。

1 个答案:

答案 0 :(得分:0)

试试这个

MariaDB [t]> CREATE TABLE myTable(points INT, age INT, x_id INT, y_id INT);
Query OK, 0 rows affected (0.03 sec)

MariaDB [t]> INSERT INTO myTable VALUES(1, 57, 1, NULL);
Query OK, 1 row affected (0.00 sec)

MariaDB [t]> INSERT INTO myTable VALUES(1, 36, NULL, 13);
Query OK, 1 row affected (0.01 sec)

MariaDB [t]> SELECT * FROM myTable;
+--------+------+------+------+
| points | age  | x_id | y_id |
+--------+------+------+------+
|      1 |   57 |    1 | NULL |
|      1 |   36 | NULL |   13 |
+--------+------+------+------+
2 rows in set (0.00 sec)

MariaDB [t]> UPDATE myTable SET points = points + 1 WHERE (x_id = 1 AND age = 57) OR (y_id = 13 AND age = 36);
Query OK, 2 rows affected (0.01 sec)
Rows matched: 2  Changed: 2  Warnings: 0

MariaDB [t]> 
MariaDB [t]> SELECT * FROM myTable;
+--------+------+------+------+
| points | age  | x_id | y_id |
+--------+------+------+------+
|      2 |   57 |    1 | NULL |
|      2 |   36 | NULL |   13 |
+--------+------+------+------+
2 rows in set (0.00 sec)