2

Have a MySQL database table with 2 columns: id, url

The url column has values like 'http://www.example.com/'.

I need to add a string like 'http://www.x.com?redirect=' to the beginning of all the column values - i.e. to change the values like this: http://www.example.com/ ===> http://www.x.com?redirect=http://www.example.com/

Anyone has some clues to how I can do that?

I have looked into using CONCAT(), but so far I haven't been able to make it work :(

Thanks a lot for your help, Louisa

1
  • 2
    Do you want to add said prefix permanently (i.e. update the table's content), or only add it in the results of a SELECT query? Moreover, does this sort of processing not more appropriately belong in the presentation layer of your application? Commented Dec 22, 2012 at 0:24

4 Answers 4

7

Using concat it would be like this:

update table set url=concat('http://www.x.com?redirect=',url);
Sign up to request clarification or add additional context in comments.

1 Comment

Fantastic! Plain and simple solution. Thanks a lot.
6

Yes, you use can use CONCAT:

SELECT CONCAT('http://www.x.com?redirect=', url) AS url
FROM yourtable

See it working online: sqlfiddle

Comments

1

You can do this:

Update myTable
SET data= (SELECT CASE WHEN data IS NULL THEN '' ELSE data END AS data WHERE id = 1) + 'some text'
WHERE id = 1

field = field + value does not work when field is null.

Comments

0

Take a look at code snippet from this SO answer:

update t set data=concat(data, 'a');

Something similar should work:

update t set data=concat('http://www.x.com?redirect=', data);

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.