SQL:通过在扩展名前添加 1,2,3... 重命名重复的文件名
我试图弄清楚如何重命名存储文件名的列的内容。目前,该字段中有重复项,我试图通过在每个重复项后添加增量整数来重命名,例如,
ID | FILENAME
----------------------
1 | file1.ext
2 | file2.ext
3 | file1.ext
4 | file1.ext
5 | file3.ext
6 | file3.ext
7 | file4.ext
因此在上面的示例中,我希望以下内容是唯一的:
ID | FILENAME
----------------------
1 | file1.ext
3 | file1.ext
4 | file1.ext
5 | file3.ext
6 | file3.ext
通过将文件名更改为:
ID | FILENAME
----------------------
1 | file1-1.ext
3 | file1-2.ext
4 | file1-3.ext
5 | file3-1.ext
6 | file3-2.ext
我知道如何找到重复,但我不确定如何在扩展之前添加增量或如何首先增加计数。
任何帮助将不胜感激。
I'm trying to figure out how to rename the contents of a column which stores a filename. Currently there are duplicates in this field which I'm trying to rename by adding a incremental integer after each duplicate, e.g.
ID | FILENAME
----------------------
1 | file1.ext
2 | file2.ext
3 | file1.ext
4 | file1.ext
5 | file3.ext
6 | file3.ext
7 | file4.ext
So in the above example, I want the following to be unique:
ID | FILENAME
----------------------
1 | file1.ext
3 | file1.ext
4 | file1.ext
5 | file3.ext
6 | file3.ext
By changing the filenames to:
ID | FILENAME
----------------------
1 | file1-1.ext
3 | file1-2.ext
4 | file1-3.ext
5 | file3-1.ext
6 | file3-2.ext
I know how to find the duplicates but I'm not sure how to add increments to just before the extension or how to increment the count in the first place.
Any help would be greatly appreciated.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用窗口函数
row_number()
获取数字并替换
字符串操纵。您没有透露您的 RDBMS。以下查询在 PostgreSQL 9.0 上测试。 MySQL 不支持窗口函数,但大多数其他大型 RDBMS 都支持。
重命名所有文件名:
仅重命名重复文件名:
使用额外请求的功能进行编辑
此版本适用于名称中的多个点或无点。在 PostgreSQL 9.0 中测试。
Use the window function
row_number()
to get the number andreplace
for the string manipulation.You did not disclose your RDBMS. The following query is tested on PostgreSQL 9.0. MySQL does not support window functions, most other big RDBMS do.
Rename all filenames:
Only rename duplicate filenames:
Edit with additionally requested features
This version works with multiple or no dots in the name. Tested in PostgreSQL 9.0.