【发布时间】:2020-02-27 12:19:09
【问题描述】:
我的名为“post”的数据库表如下所示
id | message
----------------
1 | test
2 | Here is your image link: [LINK]https://example.com/images/1234[/LINK] You can view it now.
3 | some strings
4 | Here is your image link: [LINK]https://example.com/images/5678[/LINK] You can view it now.
5 | [LINK]no correct url[/LINK]
6 | [LINK][IMG]https://example.com/images/9123[/IMG][/LINK]
7 | [LINK]https://example.com/images/912364[/LINK]
8 | [LINK]Some text https://example.com/images/23456 Text again[/LINK]
9 | [URL="https://example.com/images/10796"]
因此,并非每个消息行都包含一个 url,也不是每个带有 [LINK] 标记的消息都包含一个正确的 url。还有一些条目的 ID 较长,不应更改。
现在我必须更改 ID 长度在 4 到 5 个字符之间的每个条目:
https://example.com/images/1234
https://example.com/images/5678
到那种格式 -> 添加文件扩展名
https://example.com/images/1234.png
https://example.com/images/5678.png
所以“ID”等于文件名。仅替换 URL 并不难,但我必须添加静态文件扩展名,在我的例子中是 URL 字符串末尾的“.png”。
编辑//
最后,我的数据库表应该是这样的
id | message
----------------
1 | test
2 | Here is your image link: [LINK]https://example.com/images/1234.png[/LINK] You can view it now.
3 | some strings
4 | Here is your image link: [LINK]https://example.com/images/5678.png[/LINK] You can view it now.
5 | [LINK]no correct url[/LINK]
6 | [LINK][IMG]https://example.com/images/9123.png[/IMG][/LINK]
7 | [LINK]https://example.com/images/912364[/LINK]
8 | [LINK]Some text https://example.com/images/23456.png Text again[/LINK]
9 | [URL="https://example.com/images/10796.png"]
仅在“URL-ID”有 4 位或 5 位数字且 URL 匹配的情况下将文件扩展名添加到 URL。
我绝对不是有经验的 SQL 用户。
【问题讨论】:
-
。 .编辑您的问题并显示您想要所有行的最终结果。
-
我刚刚进行了编辑
-
是否有可能在一条消息中出现 2 个或多个 [LINK]..[/LINK]?尤其是当一个正确而另一个不正确时......
-
最后,我的数据库表应该是这样的 为什么
id=7中的链接没有更新? -
@Akina,是的,有可能出现多个 [LINK]-标签。 id=7 没有更新,因为 URL 末尾的“ID 部分”有 6 位数字,其中查询应该只更改末尾有 4 或 5 位数字的条目。