SQL:通过在扩展名前添加 1,2,3... 重命名重复的文件名

发布于 2024-12-14 05:57:13 字数 755 浏览 7 评论 0原文

我试图弄清楚如何重命名存储文件名的列的内容。目前,该字段中有重复项,我试图通过在每个重复项后添加增量整数来重命名,例如,

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

〆一缕阳光ご 2024-12-21 05:57:13

使用窗口函数 row_number() 获取数字并替换 字符串操纵
您没有透露您的 RDBMS。以下查询在 PostgreSQL 9.0 上测试。 MySQL 不支持窗口函数,但大多数其他大型 RDBMS 都支持。

重命名所有文件名

SELECT id
      ,replace(filename, '.',  
               '-'
               || row_number() OVER (PARTITION BY filename ORDER BY id)
               || '.')
FROM   mytbl

仅重命名重复文件名

SELECT id
      ,CASE WHEN (count(*) OVER (PARTITION BY filename)) > 1 THEN
          replace(filename, '.',  
                  '-'
                  || row_number() OVER (PARTITION BY filename ORDER BY id)
                  || '.')
       ELSE filename END AS filename
FROM   mytbl;

使用额外请求的功能进行编辑

此版本适用于名称中的多个点或无点。在 PostgreSQL 9.0 中测试。

SELECT id
      ,CASE WHEN (count(*) OVER (PARTITION BY filename)) > 1 THEN
          regexp_replace(filename
              -- pick the longest string from the start not 
             ,'^([^.]*)'containing a '.'
              -- and replace it with itself + row_number
             ,E'\\1-' || row_number() OVER (PARTITION BY filename ORDER BY id))
       ELSE filename END AS filename
FROM   mytbl

Use the window function row_number() to get the number and replace 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:

SELECT id
      ,replace(filename, '.',  
               '-'
               || row_number() OVER (PARTITION BY filename ORDER BY id)
               || '.')
FROM   mytbl

Only rename duplicate filenames:

SELECT id
      ,CASE WHEN (count(*) OVER (PARTITION BY filename)) > 1 THEN
          replace(filename, '.',  
                  '-'
                  || row_number() OVER (PARTITION BY filename ORDER BY id)
                  || '.')
       ELSE filename END AS filename
FROM   mytbl;

Edit with additionally requested features

This version works with multiple or no dots in the name. Tested in PostgreSQL 9.0.

SELECT id
      ,CASE WHEN (count(*) OVER (PARTITION BY filename)) > 1 THEN
          regexp_replace(filename
              -- pick the longest string from the start not 
             ,'^([^.]*)'containing a '.'
              -- and replace it with itself + row_number
             ,E'\\1-' || row_number() OVER (PARTITION BY filename ORDER BY id))
       ELSE filename END AS filename
FROM   mytbl
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文