I have the following code
$this->db->query("UPDATE customer_product_owned
SET quantity = (max(0, (quantity - " . (int)$order_product_upgrade['quantity'] . ")))
WHERE customer_id = '" . (int)$order_info['customer_id'] . "'");
I would like the php variable to be subtracted from the value of the quantity field. However if the value is less than 0 i want the value to be 0.
What am i doing wrong here? can max be used within a ‘set’? I could do this with an additional query beforehand by first finding out the value of quantity but is there a way to do it in the same query?
MAXis an aggregation function. You needGREATEST(0, ...)Don’t enclose numbers in quotes. Just specify them as is, like: