【问题标题】:SQL statement to replace final part of an URL用于替换 URL 的最后部分的 SQL 语句
【发布时间】:2019-11-16 02:27:08
【问题描述】:

用具有许多不同 URL 类型的列 URL 替换 videos 表。 我使用 MariaDB 10.3

https://google.com/questions/ask?963
https://google.com/embed/57=66.88.028.10/i/03/077fsdf
https://google.com/top57=66.88.028.10/i/03/077
https://video.net/emb.html?asdeen45dr57=66.88.028.10/i/03/07776/asdeen45dr57
https://video.net/fomdfk5f7s1f.html
https://video.net/emb.html?qsfeen4gttv1=54.47.158.810/i/11/00036/qsfeen4gttv1

我需要删除特定网址的一部分(删除=之后的所有内容) 并替换为 .html

来自

https://video.net/emb.html?asdeen45dr57=66.88.028.10/i/03/07776/asdeen45dr57

https://video.net/emb.html?asdeen45dr57.html

在这种情况下,将替换

=66.88.028.10/i/03/07776/asdeen45dr57

.html

请注意,= 符号之后的 URL 部分对于每个 URL 都不同。

【问题讨论】:

  • 什么版本的mysql?

标签: mysql sql replace phpmyadmin mariadb


【解决方案1】:

如果您运行的是 MySql 8.0 或更高版本,您可以使用regexp_replace

UPDATE videos
SET url = REGEXP_REPLACE(url, '=.*$', '.html')
WHERE url LIKE 'https://video.net%'

如果没有正则表达式,这将起作用(也适用于 MySql 5.*)

UPDATE videos 
SET url = CONCAT(SUBSTR(url, 1, INSTR(url, '=') - 1), '.html')
WHERE url LIKE 'https://video.net%'

【讨论】:

  • 但仅在URLvideo.net/emb.html? 类似时才需要替换
  • 嗯,您可以简单地将其添加为 WHERE 条件url LIKE 'https://video.net/emb.html?%'
【解决方案2】:

你可以这样做:

update t
    set url = concat(substring_index(url, '=', 1), '.html')
    where url like '%-%'

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-11-16
    • 1970-01-01
    • 2023-01-04
    • 2019-04-15
    • 1970-01-01
    • 2012-03-20
    • 1970-01-01
    • 2010-12-12
    相关资源
    最近更新 更多