0

I have 2 tables, one named pictures and another named media, in table pictures I have the field description which contains some text, In table media there is the row url that I would like to update. I need to write an sql query that updates row url in table media that uses regex for an specific string in description from table pictures, can this be done with one sql query?

1
  • Seeing your table structure and the relationships would help. Commented Jan 18, 2013 at 6:12

1 Answer 1

2

You can use REGEXP to search for strings but you cannot return the result. If that's alright then you could try something like this:

If your table is something like:

create table media(media_id int, url varchar(200));
create table pictures(pic_id int, media_id int, description text);

You could do something like this:

UPDATE pictures p
LEFT JOIN media m 
ON p.media_id = m.media_id
SET url='http://newurl.com/pic.jpg'
WHERE REGEXP '.*regexpString$';

If you need to just replace part of the URL you could try:

UPDATE pictures p
LEFT JOIN media m 
ON p.media_id = m.media_id
SET url=REPLACE(url, 'olddomain.com', 'newdomain.com')
WHERE REGEXP '.*regexpString$';
Sign up to request clarification or add additional context in comments.

3 Comments

@llion hi thanks for the response!, I need to return the response because in the field description contains part of the path that I need for updating url,
in the first code-part you wrote picture (singular) and then you wrote pictures (plural). was this on purpose?
Nope, that was a typo. Fixing it now.

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.