TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
MySQL syntax helpI 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.. |
|
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
Do you need to set them all to the same value? |
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
yes - the new value will be static |
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
to TearAbite
How about something like this? UPDATE `table` SET `column` = 'value' WHERE `id` IN ( SELECT `id` FROM `table` WHERE `column` LIKE '%Joe%' );
|
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
ok, so i know my table, and the column.. and the value (my new value, right?) but what is "id" ? |
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
to TearAbite
The column which represents your table's primary key. |
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
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%' );
|
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
to TearAbite
Bummer...my bad. I thought for sure that would work, but I just tested and got the same error. |
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
any other options/ideas? is this even possible? |
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
UPDATE node_revisions SET teaser = replace( teaser, teaser, '/files' ) WHERE teaser LIKE '%/system/files%';
|
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
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' |
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
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. |
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
looks like this will work update node_revisions set teaser = replace(teaser, '/system/files', '/files')
|
|
JAAuldeWeb Developer MVM join:2001-05-09 Frederick, MD |
Ah, well, that's simple enough. Glad you got it! |
|
TearAbiteD'oh join:2001-07-25 Rancho Cucamonga, CA |
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.. |
|