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
Mary-Kate OlsenOriginal
2024-11-06 08:28:02815browse

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;

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';

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!

Statement:
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