Take the 2-minute tour ×
Database Administrators Stack Exchange is a question and answer site for database professionals who wish to improve their database skills and learn from others in the community. It's 100% free, no registration required.

I have a corrupt blob in a row.
I'd like to replace that with another blob from the same table.

How can I do that with sql statements only? (No file system access if at all possible.)

share|improve this question

1 Answer 1

up vote 2 down vote accepted

Poor man's change:

mysql> CREATE TABLE `foo` (
  `id` tinyint(4) NOT NULL AUTO_INCREMENT,
  `content` blob,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8

mysql> INSERT INTO foo VALUES (NULL, 'bar'), (NULL, 'another_bar');

mysql> SELECT * FROM foo;
+----+-------------+
| id | content     |
+----+-------------+
|  1 | bar         |
|  2 | another_bar |
+----+-------------+
2 rows in set

mysql> SELECT content INTO @newContent FROM foo WHERE id=2;

mysql> UPDATE foo SET content=@newContent WHERE id=1;

mysql> SELECT * FROM foo;
+----+-------------+
| id | content     |
+----+-------------+
|  1 | another_bar |
|  2 | another_bar |
+----+-------------+
2 rows in set

And now I've done it without using session variables:

 UPDATE foo, (SELECT content AS selectContent FROM foo WHERE id=2)selectFoo 
  SET content=selectContent 
  WHERE id=1;
share|improve this answer
    
Not a poor man's change, just cost effective. +1 !!! –  RolandoMySQLDBA Feb 1 '12 at 16:50
    
@Rolando It's 'poor', because there seems to be a better way...which I just updated into my answer without using session variables. Thanks for the upvote! –  Derek Downey Feb 1 '12 at 16:51

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.