MySQL IF语句更新MySQL列

时间:2014-11-11 14:14:41

标签: php mysql if-statement

您好我遇到mysql的问题我并不是真的擅长mysql但是我想每分钟更新一次列,所以我每隔一刻写一个事件来更新列但是我遇到了问题码?

SELECT * FROM resources

IF wood < warehouse THEN
    UPDATE `resources` SET `wood`= wood + 10
ELSE
    UPDATE `resources` SET `wood`= wood
END IF;

IF stone < warehouse THEN
    UPDATE `resources` SET `stone`= stone + 10
ELSE
    UPDATE `resources` SET `stone`= stone
END IF;

IF metal < warehouse THEN
    UPDATE `resources` SET `metal`= metal + 10
ELSE
    UPDATE `resources` SET `metal`= metal
END IF;

我真的希望你可以做if语句,因为我想更新木头,石头,金​​属但是没有它超过仓库空间限制?有线索吗?

2 个答案:

答案 0 :(得分:5)

MySQL在UPDATE语句中支持IF条件。您的陈述可能如下所示:

UPDATE  `ressources`
SET     `wood` = IF(wood < warehouse, wood + 10, wood),
        `stone` = IF(stone < warehouse, stone + 10, stone),
        `metal` = IF(metal < warehouse, metal + 10, metal);

答案 1 :(得分:3)

你在找这样的东西吗?

UPDATE `resources` 
SET `wood`= IF(wood < warehouse, wood + 10, wood),
stone = IF(stone < warehouse, stone + 10, stone),
...;