How to relocate images

I had to code an external program, I can’t see any other way. I used Java JDBC in my case.

Query:

SELECT p.post_title, m.meta_value
FROM wp_term_taxonomy tt
INNER JOIN wp_term_relationships tr on tt.term_taxonomy_id = tr.term_taxonomy_id
inner join wp_posts p on p.ID = tr.object_id
inner join wp_posts p2 on p.ID = p2.post_parent
inner join wp_postmeta m on p2.ID = m.post_id
where tt.term_id = 795
and p.post_status="publish"
and p.post_type="post"
and p2.post_type="attachment" and p2.post_status="inherit"
and m.meta_key = '_wp_attached_file'

But was not able to copy to a specific mycat, wordpress does not recognize custom folder structure for media.

Then, as all images are in current year/month, I did:

UPDATE wp_posts p
SET p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/02/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/03/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/04/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/05/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/06/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/07/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/08/', 'http://miwp.local/wp-content/uploads/2016/04/'),
p.post_content = REPLACE(p.post_content, 'http://miwp.local/wp-content/uploads/2014/09/', 'http://miwp.local/wp-content/uploads/2016/04/')
WHERE p.post_name LIKE 'mycat-%';

provided post_name is prefixed with ‘mycat-‘ .