Jump to content

UPDATE SQL Needed


mcmuney

Recommended Posts

I'm looking for an UPDATE SQL statement that will allow me to replace a portion of the field. For example, if I have a field that has this content, "The apple is red" and I want to update only "red" with "blue".

 

Btw, this will be a mass update.

 

Thanks in advance.

Link to comment
Share on other sites

For example:

$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter is my name' ");
$row = mysql_fetch_array($result);

$replace = str_replace('Peter', 'Jammy', $row['FirstName']);

mysql_query("UPDATE Persons SET FirstName = '$replace'
WHERE FirstName = 'Peter' ");

 

Something like that should set first name to 'Jammy is my name' where 'Peter is my name' was.

 

Link to comment
Share on other sites

For example:

$result = mysql_query("SELECT * FROM Persons WHERE FirstName='Peter is my name' ");
$row = mysql_fetch_array($result);

$replace = str_replace('Peter', 'Jammy', $row['FirstName']);

mysql_query("UPDATE Persons SET FirstName = '$replace'
WHERE FirstName = 'Peter' ");

 

Something like that should set first name to 'Jammy is my name' where 'Peter is my name' was.

 

 

This SINGLE query would accomplish the same thing as that code above!

UPDATE Persons
SET FirstName = REPLACE(FirstName, 'Peter', 'Jammy')
WHERE FirstName='Peter is my name'

 

No need to query the database for the records, process the records, and then do an update.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.