Ask Your Question
1

How can I update a table in MySql using a select statement from the same table?

asked 2023-07-13 07:41:02 +0000

david gravatar image

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
2

answered 2023-07-13 08:09:01 +0000

nofretete gravatar image

To update a table in MySQL using a select statement from the same table, you can use a subquery in the UPDATE statement. Here's an example:

UPDATE tablename SET column1 = newvalue WHERE id IN (SELECT id FROM tablename WHERE somecondition);

In this example, tablename is the name of the table you want to update, column1 is the name of the column you want to update, newvalue is the new value you want to set for the column, and some_condition is the condition that the rows you want to update should meet.

The subquery in the WHERE clause selects the rows that meet the condition, and the outer query updates the column1 with the new value for those rows.

Note: Be careful when using subqueries to update a table, as they can sometimes result in unexpected results or performance issues. It's important to test your queries thoroughly before running them on production data.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account. This space is reserved only for answers. If you would like to engage in a discussion, please instead post a comment under the question or an answer that you would like to discuss

Add Answer


Question Tools

Stats

Asked: 2023-07-13 07:41:02 +0000

Seen: 11 times

Last updated: Jul 13 '23