-2

在mysql中可以在命令“SET”中创建一个fork吗?

像这样:

UPDATE `table`.`data` SET `hits` = (`hits`-1<0 ? 0:`hits`-1) WHERE `data`.`id`='15';
4

1 回答 1

2

使用IF功能

UPDATE `table`.`data`
SET `hits` = IF(`id` - 1 > 0, 0, `id` - 1)
WHERE `data`.`id`='15';

在这种特定情况下,您还可以使用LEAST.

UPDATE `table`.`data`
SET `hits` = LEAST(`id` - 1, 0)
WHERE `data`.`id`='15';

不过,您的代码似乎是错误的。我几乎可以肯定你的意思是写这个:

UPDATE yourtable
SET hits = GREATEST(0, hits - 1)
WHERE id = 15;
于 2012-07-25T22:09:00.230 回答