简体   繁体   中英

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.

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.

Is there any string I could enter in phpMyAdmin that would do the trick?

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

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 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 :

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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