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

标签 mysql database

如何通过数据库中的 MySQL 查询更改嵌入帖子中的链接结构? 例如,我想更改链接结构,如下所示:(第一行到第二行)

http://forum.site.com/thread1001.html
http://forum.site.com/index.php?app=forums&module=forums&controller=topic&id=1001

这可能吗?如果没有,还有其他办法吗? 谢谢...

最佳答案

您想在 MySQL 中进行一些字符串操作,是吗?

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

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语句:

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:

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 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 子句。

<小时/>

说明:

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 中,我们没有正则表达式替换等,没有插件/扩展(据我所知)。

关于mysql - 如何使用查询更改数据库中的链接结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32183503/

相关文章:

java - 在 Lucene 索引中搜索特定术语

mysql - 检查数据库行之一中是否存在值

database - PostgreSQL:pg_dump + psql 与模板创建之间的区别?

php - mysql两次离开加入同一个表

php - 我想从3个不同的txt文件中获取数据,然后随机显示数据

javascript - 尝试将值插入到自动生成的元素中

database - ESENT 表格浏览器?

mysql - 如何在 Codeigniter 中查找活跃用户和过期用户并比较日期?

php - 允许的内存大小为 134217728 字节 exhausted + mysql + php

php - PDO 插入错误