MySQL - Update Query - Edit Records
In MySQL, the UPDATE statement is used to modify the existing data in a table. It allows you to change the values in one or more columns of a table for one or many rows based on a specified condition. Here's the basic syntax of the UPDATE statement:
UPDATE table_name SET column1 = new_value1, column2 = new_value2, ... WHERE condition;
In the above syntax, "table_name" is the name of the table that you want to update, "column1" and "column2" are the names of the columns you want to modify, "new_value1" and "new_value2" are the new values that you want to set, and "condition" is the condition that determines which rows to update.
Here are some examples of how to use the UPDATE statement in MySQL:
Example 1: Update a single column for all rows
UPDATE users SET age = 25;
This query updates the "age" column for all rows in the "users" table and sets the value to 25.
Example 2: Update multiple columns for a single row
UPDATE users SET name = 'John', age = 30, email = '[email protected]' WHERE id = 1;
This query updates the "name", "age" and "email" columns for the row with "id" = 1 in the "users" table.
Example 3: Update multiple rows based on a condition
UPDATE users SET status = 'inactive' WHERE age > 50;
This query updates the "status" column to "inactive" for all rows in the "users" table where the "age" is greater than 50.