简体   繁体   English

将SQL列的最后N个字符复制到另一列中(又叫我搞砸了!)

[英]Copy last N characters of SQL column in another column (AKA I messed up!)

I messed up big time: I added about 2000 images to Phoca Gallery and instead of leaving the "title" field empty, so it would use the filenames as the title, I wrote the name of the category... Renaming each one manually would be a PITA, but I'm sure it can be done in SQL, except I don't really know how. 我搞砸了很多时间:我在Phoca Gallery中添加了大约2000张图像,而不是将“ title”字段留为空白,因此它将文件名用作标题,我写下了类别的名称...手动重命名每个类别成为PITA,但我确信可以用SQL完成,除非我真的不知道如何做。

What I have now is: 我现在所拥有的是:

(15574, 1379, 0, 'Thursday, 25.6.', 'thursday-25-6', 'competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg', 1, NULL, '2015-07-01 16:55:11', 0, '', '', 0, '', 0, NULL, 0, 212111, 1, 1, 0, '0000-00-00 00:00:00', 13, NULL, NULL, NULL, NULL, NULL, NULL, '', 0, '', '', '', '', '', '', '*'),

and what I'd need is: 我需要的是:

(15574, 1379, 0, '_MG_5545.jpg', '_mg_5545.jpg', 'competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg', 1, NULL, '2015-07-01 16:55:11', 0, '', '', 0, '', 0, NULL, 0, 212111, 1, 1, 0, '0000-00-00 00:00:00', 13, NULL, NULL, NULL, NULL, NULL, NULL, '', 0, '', '', '', '', '', '', '*'),

so I'd replace "Thursday, 25.6" and it's alias "thursday-25-6" with the filename of each image, which is the last 12 digits of the column with the image location. 因此,我将“ Thursday,25.6”替换为别名“ thursday-25-6”,并将其替换为每张图片的文件名,这是图片位置所在列的最后12位数字。

Is there any string I could enter in phpMyAdmin that would do the trick? 我可以在phpMyAdmin中输入任何可以解决问题的字符串吗?

Thank you so much! 非常感谢!

If the pattern is same ie competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg' Then you may use the substring_index function and using this you do not have to rely on 12 characters rather you can extract the last part of string after the last / something as 如果模式相同,即competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg'则可以使用substring_index函数,使用该函数不必依赖12 characters而是可以提取字符串后的最后一部分最后/作为

mysql> select substring_index('competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg','/',-1) as i ;
+--------------+
| i            |
+--------------+
| _MG_5545.jpg |
+--------------+
1 row in set (0.00 sec)

mysql> select lower(substring_index('competitions/hrprvprj/25.6/120/vucemilo_filip/_MG_5545.jpg','/',-1)) as i ;
+--------------+
| i            |
+--------------+
| _mg_5545.jpg |
+--------------+
1 row in set (0.00 sec)

So you may have the update command as 因此,您可能将update命令作为

update tablename 
set 
col1 = substring_index(col_file_path,'/',-1),
col2 = lower(substring_index(col_file_path,'/',-1));

You can use the substring function to extract the last 12 characters of the string, and from there on it's just a plain old update : 您可以使用substring函数提取substring的最后12个字符,从那里开始,这只是一个简单的旧update

UPDATE mytable
SET    title = SUBSTRING(filename, -12),
       alias = LOWER(SUBSTRING(filename, -12))
WHERE  <some condition>

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

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