0

I want to write migrate script for my word-press blog in mysql with single statement if possible

here is sudo code what i want to do.

SELECT post_id, meta_value FROM wp_postmeta WHERE meta_key =  'product' as olddata;
foreach {
UPDATE wp_postmeta SET meta_value=olddata.post_id WHERE meta_key = 'myslug_product' AND meta_value = olddata.meta_value 
}

I know it can be done with any programming language. But i want sql solution.

5
  • Did you try a google search for SQL UPDATE SELECT? Commented Nov 6, 2014 at 13:53
  • yes i did found they fires two or there times update depending on where condition. Commented Nov 6, 2014 at 14:09
  • This question found Commented Nov 6, 2014 at 14:13
  • 2
    If you find an answer, then simply delete your question. Or it may be further downvoted or even marked as duplicate and auto-closed via review-queue. If you lack something, then clearly describe your intention and what problem you can't fix yourself. Commented Nov 6, 2014 at 14:35
  • okay :/ can't delete it :\ Commented Nov 7, 2014 at 10:25

1 Answer 1

1

Why use "for each" when you can do an update join. In my opinion it is a lot cleaner.

UPDATE wp_postmeta 
INNER JOIN wp_postmeta olddata 
ON meta_value = olddata.meta_value and
   olddata.meta_key =  'product'
SET meta_value=olddata.post_id
WHERE meta_key = 'myslug_product'
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.