Home > Database > Mysql Tutorial > How to Update a Specific Column in MySQL Without Affecting Other Columns?

How to Update a Specific Column in MySQL Without Affecting Other Columns?

Mary-Kate Olsen
Release: 2024-11-06 08:28:02
Original
839 people have browsed it

How to Update a Specific Column in MySQL Without Affecting Other Columns?

Updating a Column in MySQL

The goal is to update the values in a specific column ("col_a") of a table ("table1") while leaving other columns ("col_b") untouched. Attempting to use the INSERT statement to achieve this won't work.

Solution:

To update a selected column, you must utilize the UPDATE statement. The syntax is as follows:

UPDATE table_name SET column_name = new_value WHERE condition;
Copy after login

Example:

To set the values of "col_a" for rows with specific "key_col" values, use the following query:

UPDATE table1 SET col_a = 'foo' WHERE key_col = 'k1';
UPDATE table1 SET col_a = 'bar' WHERE key_col = 'k2';
Copy after login

The above is the detailed content of How to Update a Specific Column in MySQL Without Affecting Other Columns?. 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