[英]Replace substring in one column with value from another column in same row
I have a MySQL database with two columns, id
and file
: 我有一个MySQL数据库,其中有两列,
id
和file
:
id file
1 /img/333.PNG
2 /img/452645.jpg
3 /img/1.gif
The file
s are all the same path ( /img/
), a number, and a graphics file extension, some in lower ( .png
), some in upper case ( .JPEG
). file
都具有相同的路径( /img/
),数字和图形文件扩展名,其中一些位于小写( .png
)中,而某些则大写( .JPEG
)。
file
with the id
(from the same row)? file
与id
(来自同一行)? The result for the above sample should look like: 以上示例的结果应类似于:
id file
1 1.PNG
2 2.jpg
3 3.gif
Notes: 笔记:
file
in all rows using only one query. file
。 This should work 这应该工作
UPDATE table SET file = CONCAT(id, '.', RIGHT(file, 3));
To accommodate JPEG extension locate position of dot. 要容纳JPEG扩展名,请找到点的位置。
UPDATE table SET file = CONCAT(id, SUBSTRING(file, POSITION('.' IN file)));
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.