简体   繁体   English

使用正则表达式的UPDATE SQL语句

[英]UPDATE SQL statement using regex

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. 我有2个表,一个命名为图片,另一个命名为媒体,在表pictures我具有包含一些文本的字段description ,在表media有我要更新的行url 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? 我需要编写一个sql查询来更新表媒体中的行url该媒体使用regex作为表picturesdescription的特定字符串,可以用一个sql查询来完成吗?

You can use REGEXP to search for strings but you cannot return the result. 您可以使用REGEXP搜索字符串,但不能返回结果。 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$';

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM