views:

26

answers:

2

I got this SQL:

UPDATE users2 
SET picture = 'sites/site2/files/pictures/' + picture;
WHERE picture NOT IS NULL

And the only thing I get are that all picture fields get the value '0'.

+2  A: 

In MySQL + means numerical addition. Your strings are being cast to integers, added, and then the result converted back to a string.

Use CONCAT for string concatenation. Here's a fixed version of your query:

UPDATE users2 
SET picture = CONCAT('sites/site2/files/pictures/', picture)
WHERE picture IS NOT NULL
Mark Byers
+3  A: 

Because adding does not work for strings. Use CONCAT() instead:

UPDATE users2 
SET picture = CONCAT('sites/site2/files/pictures/', picture)
WHERE pictures NOT IS NULL

Also, notice you have a semi-colon in the middle of the query... remove it or you'll get all rows updated!

Seb
+1 good spot on that semi-colon!
Mark Byers
yes thank you! and that `;` was a nice catch. It still isn't working because it doesn't like `NOT IS NULL`...ideas?
WmasterJ
@WmasterJ: NOT IS NULL -> IS NOT NULL
Mark Byers
Thank you Mark. Had just changed it to `!= ''`. But I believe both would have worked.
WmasterJ