Jump to content

Recommended Posts

mySQL ver: 5.1.30

PHP  ver: 5.2.8

 

I have a piece of code that completes but does nothing.

 

dbase: buses

Table : st

 

Fields used in the code

Seats: CHR (8)

Body:  CHR (22)

UPDATE st SET seats = REPLACE(seats, 'H29/20R ', 'H25/27RO') WHERE body LIKE 'Thomas Tilling'

 

Records 1140

Records to be changed 300

 

As I hope you can see I am trying to replace  'H29/20R ' in seats  WHEN  'Thomas Tilling' occurs in the body field

 

You help would be  much appreciated

Link to comment
https://forums.phpfreaks.com/topic/259252-my-code-runs-but-does-not-work/
Share on other sites

don't use REPLACE in an UPDATE query, it's only for SELECT.  Just include the value that you want the field to contain when you have run the UPDATE:

UPDATE st SET seats = 'H25/27RO' WHERE body LIKE 'Thomas Tilling'

 

As an afterthought, if you know the exact contnent of the field that you are looking up in the WHERE clause, use = instead of LIKE, it's more efficient:

UPDATE st SET seats = 'H25/27RO' WHERE body = 'Thomas Tilling'

Thank you very much that fixed it with ease.

 

Trouble for me is When  I google "mySQL + REPLACE"  every simple looking answer comes up with

UPDATE st SET seats = REPLACE(seats, 'H29/20R ', 'H25/27RO' ) WHERE body LIKE 'Thomas Tilling';

 

And the even simpler (I assume) global replace turns up

UPDATE st set seats = REPLACE(seats, 'H29/20R' , '(H27/25RO' )

 

Both you will notice use UPDATE and REPLACE in the queries, no wonder I hit so many hurdles when trying to find answers

If you could show me the correct syntax for a global replace that would also help a lot.

 

Regards  Topshed

the only thing that dictates which fields in the column are changed is the WHERE clause.  if there is no WHERE clause then all records will have their value updated in the column that you SET, so to perform a "global replace" as you call it simply leave off the WHERE.

UPDATE st SET seats = 'H25/27RO'

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.