mysql - 将 SQL 列的最后 N 个字符复制到另一列(也就是我搞砸了!)

标签 mysql phpmyadmin

我搞砸了大事:我向 Phoca Gallery 添加了大约 2000 张图像,而不是将“标题”字段留空,所以它会使用文件名作为标题,我写下了类别的名称...重命名每个一个手动将是 PITA,但我确信它可以用 SQL 完成,除非我真的不知道如何。

我现在拥有的是:

(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, '', '', '', '', '', '', '*'),

我需要的是:

(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, '', '', '', '', '', '', '*'),

所以我会用每个图像的文件名替换“星期四,25.6”及其别名“thursday-25-6”,这是带有图像位置的列的最后 12 位数字。

我可以在 phpMyAdmin 中输入任何字符串来解决问题吗?

非常感谢!

最佳答案

如果模式相同,即 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)

因此您可以将更新命令设为

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

关于mysql - 将 SQL 列的最后 N 个字符复制到另一列(也就是我搞砸了!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31177506/

相关文章:

mysql - Excel - 在来自 MySQL 数据库查询中使用变量单元格值 (Sheet1!A2)

php - 我的 CodeIgniter Controller 在以 id 作为参数传递时始终只采用一个特定的 id 吗?

mysql - 如何使用 MYSQL 将同名的所有行连接到一个表中?

mysql - Fedora phpMyAdmin "Cannot log in to the MySQL server",但 MySql 命令行没问题

php - WAMP - phpMyAdmin [错误 #2002]

mysql - Rails 4 数据库表

mysql - 如何在 MySql 中进行日期时间比较时小于等于?

MySQl 错误 #1064 带有多个 SELECT 语句的存储过程 IF ELSE

php - 如何使用 bitnami/linux 终端更改我的 php 应用程序的 php 版本

mysql - 我的数据库出错,但我不知道出了什么问题