Home > Database > Mysql Tutorial > How to Update Rows and Get Updated IDs in MySQL Without a Subquery?

How to Update Rows and Get Updated IDs in MySQL Without a Subquery?

Barbara Streisand
Release: 2024-10-26 20:31:02
Original
641 people have browsed it

How to Update Rows and Get Updated IDs in MySQL Without a Subquery?

Combining SELECT and UPDATE Queries in MySQL

Combining SELECT and UPDATE queries into a single operation can be useful for optimizing database performance. In this case, a user wishes to combine the following queries:

SELECT * FROM table WHERE group_id = 1013 and time > 100;
Copy after login
UPDATE table SET time = 0 WHERE group_id = 1013 and time > 100
Copy after login

While a subquery approach may not provide the desired results, there is a solution that can achieve the goal without a subquery. This solution involves using a user-defined variable (@uids) to store the updated row IDs:

UPDATE footable
   SET foo = 'bar'
 WHERE fooid > 5
   AND ( SELECT @uids := CONCAT_WS(',', fooid, @uids) );
SELECT @uids;```
Copy after login

The above is the detailed content of How to Update Rows and Get Updated IDs in MySQL Without a Subquery?. 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