user1455655
user1455655

Reputation:

Using SELECT with UPDATE in the same query

I'm trying to run an UPDATE query on SELECT results but can't figure out how, for example - i'm trying to find all numbers in a certain db that start with "888" and out of them i want to change all the results with "999052" to "052". to get the first part i can use

SELECT * FROM `csv_confirmed` WHERE mobile LIKE '999%'

This will indeed give me a list of all items that begin with 999. And the following query:

UPDATE csv_confirmed SET mobile = REPLACE(mobile, '999052', '052');

Would replace all the items that with 999052 into 052 BUT, it will not be limited to numbers that begin nor will it limit to the first query results - how do I combine between the two ?

Upvotes: 1

Views: 62

Answers (2)

Moose Morals
Moose Morals

Reputation: 1648

UPDATE csv_confirmed SET mobile ='052' WHERE mobile = '999052'; 

You can (and should) add a WHERE clause to an UPDATE to limit its effects.

Upvotes: 1

NoGray
NoGray

Reputation: 1149

UPDATE csv_confirmed SET mobile = REPLACE(mobile, '999052', '052') WHERE mobile LIKE '999%'

Upvotes: 1

Related Questions