首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何使用查询更改数据库中的链接结构

问如何使用查询更改数据库中的链接结构
EN

Stack Overflow用户
提问于 2015-08-24 13:25:13
回答 1查看 56关注 0票数 0

如何通过数据库中的MySQL查询改变嵌入在帖子中的链接结构?例如,我希望像这样更改链接结构:(第一行到第二行)

代码语言:javascript
复制
http://forum.site.com/thread1001.html
http://forum.site.com/index.php?app=forums&module=forums&controller=topic&id=1001

这个是可能的吗?如果没有,还有别的办法吗?谢谢..。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-24 13:58:23

你想在MySQL中做一些字符串操作,是吗?

https://dev.mysql.com/doc/refman/5.6/en/string-functions.html

代码语言:javascript
复制
SELECT
  CONCAT(
    SUBSTRING_INDEX(url_field, "/", 3),
    "/index.php?app=forums&module=forums&controller=topic&id=",
    SUBSTRING(
      SUBSTRING_INDEX(
        SUBSTRING_INDEX(url_field, "/", -1),
        ".",
        1
      ),
      7
    )
  )
;

要使其成为UPDATE语句,请执行以下操作:

代码语言:javascript
复制
UPDATE table_name
SET url_field = 
  CONCAT(
    SUBSTRING_INDEX(url_field, "/", 3),
    "/index.php?app=forums&module=forums&controller=topic&id=",
    SUBSTRING(
      SUBSTRING_INDEX(
        SUBSTRING_INDEX(url_field, "/", -1),
        ".",
        1
      ),
      7
    )
  )
;

如果您已经将这个URL嵌入到TEXT或其他东西中(比如用户评论),那么您必须做更多的工作才能从其中提取URL:

代码语言:javascript
复制
SELECT
  CONCAT(
    SUBSTRING_INDEX(@url_field, "http://", 1),
    "http://",
    SUBSTRING_INDEX(SUBSTRING_INDEX(@url_field, "http://", -1), "/", 1),
    "/index.php?app=forums&module=forums&controller=topic&id=",
    SUBSTRING(
      SUBSTRING_INDEX(
        SUBSTRING_INDEX(@url_field, "/", -1),
        ".",
        1
      ),
      7
    ),
    SUBSTRING_INDEX(SUBSTRING_INDEX(@url_field, "http://", -1), ".html", -1)
  )
;

或UPDATE

代码语言:javascript
复制
UPDATE table_name
SET text_field =
  CONCAT(
    SUBSTRING_INDEX(text_field, "http://", 1),
    "http://",
    SUBSTRING_INDEX(SUBSTRING_INDEX(text_field, "http://", -1), "/", 1),
    "/index.php?app=forums&module=forums&controller=topic&id=",
    SUBSTRING(
      SUBSTRING_INDEX(
        SUBSTRING_INDEX(text_field, "/", -1),
        ".",
        1
      ),
      7
    ),
    SUBSTRING_INDEX(SUBSTRING_INDEX(text_field, "http://", -1), ".html", -1)
  )
;

恐怕只有在SQL中才会越来越复杂。(您可以优化上面的内容。)

将更改记录的数量保持在最低限度--确保在结尾处有一个好的WHERE子句。

解释:

代码语言:javascript
复制
SET @url_field :=  "abcdedef http://forum.site.com/thread1001.html xyz";
SELECT
  -- CONCAT(
    SUBSTRING_INDEX(@url_field, "http://", 1), -- Everything left of URL
    "http://", -- Restore the http:// lost in the substring above
    SUBSTRING_INDEX(SUBSTRING_INDEX(@url_field, "http://", -1), "/", 1), -- Get the website domain name
    "/index.php?app=forums&module=forums&controller=topic&id=", -- Append the parts of our new URL we know won't change
    SUBSTRING(
      SUBSTRING_INDEX(
        SUBSTRING_INDEX(@url_field, "/", -1), -- Get the ID from the original URL - starting at final /
        ".", -- ... stopping at .html
        1
      ),
      7 -- ... dropping the word "thread" from the start
    ),
    SUBSTRING_INDEX(SUBSTRING_INDEX(@url_field, "http://", -1), ".html", -1) -- capture everything after the URL
  -- )
;

请注意,这可能会破坏其他URL。不幸的是,在MySQL中,没有插件/扩展(据我所知),我们就没有regex替换等。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/32183503

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档