如何在mysql中删除字符串的一部分? [英] How to remove part of string in mysql?

查看:387
本文介绍了如何在mysql中删除字符串的一部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在数据库的一个表中,我有一个看起来像这样的字符串:

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

sometext-othertext

如何使用SELECT语句删除包含破折号的文本,使结果仅为sometext?

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

推荐答案

在第一次出现定界符-"之前返回子字符串:

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

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

输出结果="foo"

Outputs result = "foo"

您可以在获取子字符串之前用所需的出现次数替换1

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

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

输出结果="foo-bar"

Outputs result = "foo-bar"

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

这篇关于如何在mysql中删除字符串的一部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆