Updating a Database Column by Subtracting a Value with MySQL
In MySQL, updating a database column by subtracting a value is straightforward. However, there's a crucial step you need to keep in mind to ensure a successful update: removing single quotes around the calculation.
Consider this example query:
UPDATE `a75ting`.`username` SET `points` = '`points` - 5'
This query won't work as intended because the single quotes around "points - 5`" transform the expression into a plaintext string. Instead, remove the single quotes to reference the "points" field and subtract 5 from its current value:
UPDATE a75ting.username SET points = points - 5
With this corrected query, MySQL will correctly update the "points" column by subtracting 5 from each record's current value. Remember, when updating a column with calculations, including arithmetic operators like subtraction, skip the single quotes around the calculation.
The above is the detailed content of How Can I Subtract a Value from a MySQL Database Column Without Errors?. For more information, please follow other related articles on the PHP Chinese website!