简体   繁体   English

用同一行中另一列的值替换一列中的子字符串

[英]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数据库,其中有两列, idfile

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 )。

How can I replace the path and numeric part of file with the id (from the same row)? 我怎么能代替路径和数字部分fileid (来自同一行)?

The result for the above sample should look like: 以上示例的结果应类似于:

id    file
1     1.PNG
2     2.jpg
3     3.gif

Notes: 笔记:

  • I don't want to rename the actual files, just the database entries. 我不想重命名实际文件,仅重命名数据库条目。
  • I want to rename 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.

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