[英]MySQL statement UPDATE IF equal to or greater than else do nothing
I have the following MySQL statement: 我有以下MySQL语句:
$stmt = $conn->prepare('UPDATE `equipment` SET `currentHours`= :unitHours, `lastUpdate`= :lastUpdate WHERE `equipType`= :equipType AND `unitNumber`= :unitNumber');
$stmt ->execute(array(':lastUpdate'=> $dateToday, ':unitHours' => $unitHours, ':equipType'=> $equipType, ':unitNumber'=> $unitNumber));
I need to only update the currentHours
if the value is equal to or greater than the value currently stored in that row. 如果该值等于或大于当前存储在该行中的值,则仅需要更新currentHours
。 Is this possible with a MySQL statement or do I have to do it in PHP? MySQL语句有可能吗?还是必须用PHP做到这一点? I am new to programming so sorry in advance if this sounds like a simple question. 我是编程新手,所以如果听起来像是一个简单的问题,请提前抱歉。
UPDATE `equipment` SET `currentHours`= :unitHours, `lastUpdate`= :lastUpdate
WHERE `equipType`= :equipType AND `unitNumber`= :unitNumber
AND
currentHours<:unitHours
The above updates only records with current hours lower than the input, it won't update all last update field that matches the rest of the condition. 上面的更新仅记录当前小时数少于输入小时数的记录,它不会更新所有与该条件的其余部分匹配的最后更新字段。
If u want to update the field currenthours only when it is lower than the input, but update the lastupdate field regardless, then: 如果您仅想在字段 currenthours小于输入时才更新它,而无论如何都更新lastupdate字段,则:
UPDATE `equipment`
SET `currentHours`= IF(currentHours<:unitHours,:unitHours,currentHours),
`lastUpdate`= :lastUpdate
WHERE `equipType`= :equipType AND `unitNumber`= :unitNumber
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.