dslreports logo
Search similar:


uniqs
497

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

MySQL syntax help

I am trying to update all the records in a table that contains a string LIKE something..
So..
if this pulls the records i want to change:
SELECT *  FROM `pages` WHERE `body` LIKE '%Joe%'
 

how would i do the equivalent of a REPLACE like this:
update pages set body = replace(body, "Joe", "Ralph"
 

what i want to do is change everything that contains "joe" (joe, joeseph, joeclyn) to something else..

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde

MVM

Do you need to set them all to the same value?

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

yes - the new value will be static

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde to TearAbite

MVM

to TearAbite
How about something like this?
UPDATE `table` SET `column` = 'value' WHERE `id` IN ( SELECT `id` FROM `table` WHERE `column` LIKE '%Joe%' );
 

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

ok, so i know my table, and the column.. and the value (my new value, right?) but what is "id" ?

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde to TearAbite

MVM

to TearAbite
The column which represents your table's primary key.

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

ok.. but this is what i get:
#1093 - You can't specify target table 'node_revisions' for update in FROM clause 
 

this is the actual line of code:
UPDATE `node_revisions` SET `teaser` = '/files' WHERE `vid` IN ( SELECT `vid` FROM `node_revisions` WHERE `teaser` LIKE '%/system/files%' );
 

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde to TearAbite

MVM

to TearAbite
Bummer...my bad. I thought for sure that would work, but I just tested and got the same error.

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

any other options/ideas?
is this even possible?

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde

MVM

UPDATE node_revisions SET teaser = replace( teaser, teaser, '/files' ) WHERE teaser LIKE '%/system/files%';
 

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

ok.. and now i feel a bit (more) like a fool.. but when i said the replacement was 'static', i meant that the replacement string would always be the same-
so if before 'teaser'="this is the full path /system/files"
the after should be "this is the full path /files"

but the above changes the entire teaser field to '/files'

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde

MVM

Ah, yes, that is why I asked. I don't know how to do it dynamically off the top of my head. It will take a decent bit of googling. As you can tell from my originally leading you astray, I am not terribly skilled in the DB world.

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

looks like this will work
update node_revisions set teaser = replace(teaser, '/system/files', '/files')
 

JAAulde
Web Developer
MVM
join:2001-05-09
Frederick, MD

JAAulde

MVM

Ah, well, that's simple enough. Glad you got it!

TearAbite
D'oh
join:2001-07-25
Rancho Cucamonga, CA

TearAbite

Member

said by JAAulde:

Ah, well, that's simple enough. Glad you got it!

took me a while with Google and you got me started in the right direction.. thanks for your help..