Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Update a column in MySQL

I have a table table1 with three columns and a bunch of rows:

[key_col|col_a|col_b] 

I want to update col_a with a set of values (i.e. leaving col_b unchanged), something like this:

INSERT INTO table1 AS t1 (key_col, col_a) VALUES ("k1", "foo"), ("k2", "bar"); 


But it doesn't work, how do I do this?

like image 921
Muleskinner Avatar asked Jun 28 '11 08:06

Muleskinner


People also ask

How do I UPDATE a column in MySQL?

The syntax to modify a column in a table in MySQL (using the ALTER TABLE statement) is: ALTER TABLE table_name MODIFY column_name column_definition [ FIRST | AFTER column_name ]; table_name. The name of the table to modify.

How do you UPDATE a single column in SQL?

First, specify the table name that you want to change data in the UPDATE clause. Second, assign a new value for the column that you want to update. In case you want to update data in multiple columns, each column = value pair is separated by a comma (,). Third, specify which rows you want to update in the WHERE clause.

How do you UPDATE a specific column?

The UPDATE statement in SQL is used to update the data of an existing table in database. We can update single columns as well as multiple columns using UPDATE statement as per our requirement. UPDATE table_name SET column1 = value1, column2 = value2,...

What is the command for UPDATE in MySQL?

UPDATE `table_name` is the command that tells MySQL to update the data in a table . SET `column_name` = `new_value' are the names and values of the fields to be affected by the update query. Note, when setting the update values, strings data types must be in single quotes.


1 Answers

You have to use UPDATE instead of INSERT:

  • UPDATE Syntax

For Example:

UPDATE table1 SET col_a='k1', col_b='foo' WHERE key_col='1'; UPDATE table1 SET col_a='k2', col_b='bar' WHERE key_col='2'; 
like image 113
Naveed Avatar answered Oct 06 '22 01:10

Naveed