Home > Database > Mysql Tutorial > How Can I Subtract a Value from a MySQL Database Column Without Errors?

How Can I Subtract a Value from a MySQL Database Column Without Errors?

Mary-Kate Olsen
Release: 2024-12-20 12:08:11
Original
494 people have browsed it

How Can I Subtract a Value from a MySQL Database Column Without Errors?

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'
Copy after login

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
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template