【问题标题】:How to cut part of a string in MySQL?如何在 MySQL 中剪切部分字符串?
【发布时间】:2012-06-20 07:18:41
【问题描述】:

在 MySQL 中,我有一个带有“bla bla bla YYY=76767 bla bla bla”的文本列。

我需要删掉76767这个号码。

如何在 SQL 中做到这一点?

【问题讨论】:

标签: mysql sql select


【解决方案1】:

你可以使用

select substring_index(substring(mycol, instr(mycol, "=")+1), " ", 1)

获取=之后的第一个令牌。

这将返回76767


这分两步进行:

substring(mycol, instr(mycol, "=")+1)

返回=之后开始的字符串

substring_index( xxx , " ", 1)

得到你从“”分割得到的虚拟数组的第一个元素,因此返回xxx的第一个标记。

【讨论】:

  • 更抽象/一般:将@frst@scnd 设置为分隔符。您要查找的术语介于 @frst@scnd 之间。在这种情况下,它是 SET @frst:='YYY='; SET @scnd:=''; 你要么想要更新,所以 UPDATE tbl set my_col = SUBSTRING_INDEX(SUBSTRING(my_col , INSTR(my_col , @frst)+1), @scnd, 1) WHERE my_col LIKE '%@scnd%'; 或者你想要选择,所以 SELECT SUBSTRING_INDEX(SUBSTRING(my_col , INSTR(my_col , @frst)+1), @scnd, 1) from WHERE my_col LIKE '%@scnd%';
【解决方案2】:

消除您领域中的数字的最简单方法就是REPLACE它。

SELECT REPLACE(yourcolumn, '76767', '')
FROM yourtable;

您也可以使用SUBSTRINGINSTR 的组合。

【讨论】:

    【解决方案3】:

    如果您不想更新,请使用

    UPDATE table_name SET column_name = REPLACE(column_name, '76767', '');
    

    如果您不想在数据库中将 76767 替换为 bla bla bla,请使用

    UPDATE table_name SET column_name = REPLACE(column_name, '76767', 'bla bla bla');
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 2011-04-14
      • 2015-07-17
      • 1970-01-01
      相关资源
      最近更新 更多