MySQL 如何删除mysql中的部分字符串?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7481361/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 21:07:09  来源:igfitidea点击:

How to remove part of string in mysql?

mysql

提问by jingo

In one table of my database I have strings which looks like this one:

在我的数据库的一张表中,我有如下所示的字符串:

sometext-othertext

How to remove the text including dash with SELECT statement so the result to be just sometext?

如何使用 SELECT 语句删除包括破折号的文本,以便结果只是sometext

回答by Bluewind

Return the substring before the first occurrence of the delimiter "-":

返回第一次出现分隔符“-”之前的子字符串:

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 1) as result;

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 1) as result;

Outputs result = "foo"

输出结果 = "foo"

You can replace 1 with the numbers of occurrences you want before getting the substring

在获取子字符串之前,您可以将 1 替换为您想要的出现次数

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 2) as result;

SELECT SUBSTRING_INDEX('foo-bar-bar', '-', 2) as result;

Outputs result = "foo-bar"

输出结果 = "foo-bar"

Reference: http://dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

参考:http: //dev.mysql.com/doc/refman/5.0/en/string-functions.html#function_substring-index

回答by zarun

SELECT REPLACE("sometext-othertext","-", "") as newvalue

SELECT REPLACE("sometext-othertext","-", "") 作为新值

This should do it.
Edit:

这应该这样做。
编辑:

  SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1) as newvalue;

apologies for not understanding the question earlier.

很抱歉没有早点理解这个问题。

回答by Abdul Aziz Al Basyir

have two ways to use troubleshooting this, if you like to display all (only just remove "-" symbol, i recommended use this because have more speed:

有两种方法可以解决此问题,如果您想显示所有内容(只需删除“-”符号,我建议使用它,因为速度更快:

SELECT REPLACE('sometext-othertext','-','');

that syntax change text "-" to "" (empty text because you want just remove that)

该语法将文本“-”更改为“”(空文本,因为您只想删除它)

if you want to display only part one (ex :sometext) or just part two (ex :othertext) you can use this, for spliting string:

如果您只想显示第一部分(例如:sometext)或仅显示第二部分(例如:othertext),您可以使用它来分割字符串:

SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1);

回答by Abdul Aziz Al Basyir

just use :

只需使用:

SELECT SUBSTRING_INDEX('sometext-othertext', '-', 1);

Function have 3 prameters, first that is a text, second is object for spliting, and number (1) for getting you want (the controller)

函数有3个参数,第一个是文本,第二个是拆分对象,数字(1)让你想要(控制器)

回答by DhruvPathak

better use mysql REPLACE function to replace "-" with ""

更好地使用mysql REPLACE函数将“-”替换为“”