mysql根据字段值更新行

时间:2012-01-06 21:54:17

标签: mysql

如何根据以前的值更新mysql中的字段?

让我们说count等于3。

UPDATE MY_TABLE SET count = count-1

那会有用吗?所以count的新值将是4

2 个答案:

答案 0 :(得分:0)

你的意思是排还是桌子?如果您的意思是插入或更新一行,您可以使用triggers

答案 1 :(得分:0)

我试了一下它有用,所以也许你需要向我们展示表的CREATE语句。测试结果:

mysql> create table aint ( count int not null default 0 );
Query OK, 0 rows affected (0.25 sec)

mysql> insert into aint VALUES(1);
Query OK, 1 row affected (0.06 sec)

mysql> select * from aint;
+-------+
| count |
+-------+
|     1 |
+-------+
1 row in set (0.00 sec)

mysql> update aint set count = count-1;
Query OK, 1 row affected (0.06 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from aint;
+-------+
| count |
+-------+
|     0 |
+-------+
1 row in set (0.00 sec)

mysql> update aint set count = count-1;
Query OK, 1 row affected (0.00 sec)
Rows matched: 1  Changed: 1  Warnings: 0

mysql> select * from aint;
+-------+
| count |
+-------+
|    -1 |
+-------+
1 row in set (0.00 sec)