首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >MySQL语句中的SUBSTR两次;

MySQL语句中的SUBSTR两次;
EN

Stack Overflow用户
提问于 2012-08-23 09:22:57
回答 2查看 440关注 0票数 0

所以我的回答是这样的。“恭喜!您在***.”*中赢得了松下音响系统。我们的呼叫中心将在*上与您联系。我需要的是只选择奖(在这种情况下松下音响系统“)作为输出。不同的奖项的字符不同。其他有许多字符,只有10个字符。我需要运行一个select语句,删除两个领先的”恭喜!你在**中赢得了“和尾随”。我们的呼叫中心将通过****.“与您联系。因此,还奖;让我们调用我的表条目,我的字段,其中有这个文本,我们称之为响应;我已经跑了SELECT SUBSTR(response,32) from entries; and I remove the leading characters before the prize.

当我运行这个,我得到“松下声音系统在****.";*。我们的呼叫中心会联系您在*。”

引线的字符长度为32,尾数为94。奖品不是固定不变的。

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2012-08-23 09:32:43

在这里,索引可能很有用:

代码语言:javascript
复制
select 
substring_index(
substring_index(
"Congratulations! You have won a Panasonic Sound System in the *. Our call centre will contact you on ***."," a ",-1)," in the ",1);
票数 0
EN

Stack Overflow用户

发布于 2012-08-23 09:53:51

如果您确信尾随字符数和前导字符计数总是恒定的,则可以使用亚斯特尔氏用例:SUBSTR(str,pos,len).

len可以通过从原始字符串的长度减去前导不想要的字符和跟踪不想要的字符来确定。

代码语言:javascript
复制
set @test_string = "Congratulations! You have won a Panasonic Sound System in the ************************. Our call centre will contact you on *************************";
set @leading = 32;
set @trailing = 94;
select substr(@test_string, @leading, length(@test_string) - @leading - @trailing);

例:

代码语言:javascript
复制
mysql> select substr(@test_string, @trailing_chars, length(@test_string) - @leading_chars - @trailing_chars);
+------------------------------------------------------------------------------------------------+
| substr(@test_string, @trailing_chars, length(@test_string) - @leading_chars - @trailing_chars) |
+------------------------------------------------------------------------------------------------+
|  Panasonic Sound System                                                                        |
+------------------------------------------------------------------------------------------------+
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/12088545

复制
相关文章

相似问题

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