【问题标题】:SUBSTR twice in MySQL statement;MySQL 语句中的 SUBSTR 两次;
【发布时间】:2012-08-18 18:53:07
【问题描述】:

所以我的回答是这样的......“恭喜!您在*中赢得了松下音响系统。我们的呼叫中心将通过与您联系***。”我需要的是仅选择奖品(在本例中为 Panasonic Sound System" )作为输出。不同奖品的字符不同。其他有很多字符,只有 10 个字符。我需要运行一个选择语句,删除两个领导“恭喜!您在 ** 中赢得了一个 " 和后面的 "。我们的呼叫中心将在 **** 上与您联系。”并因此返回奖品;让我们调用我的表格条目,我的字段包含此文本,我们称之为响应;我已经运行SELECT SUBSTR(response,32) from entries; and I remove the leading characters before the prize.

当我运行此程序时,我会在 * 中看到“松下音响系统。我们的呼叫中心将通过 ** 与您联系**.";

LEADING字符长度为32,TRAILING字符长度为94。奖品不固定。

【问题讨论】:

  • 我不确定我是否理解您的问题。

标签: mysql character substr trailing


【解决方案1】:

SUBSTRING_INDEX 可能在这里有用:

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);

【讨论】:

  • 非常感谢你们的帮助。尝试了 Jon 的建议,但出现了错误,所以我决定尝试 Tom Mac 的。第一次尝试工作。 Muchas Gracias Senor!!
【解决方案2】:

如果您确信您的尾随和前导字符数始终保持不变,您可以使用 substr's 用例:SUBSTR(str,pos,len).

len 可以通过从原始字符串的长度中减去前导不需要的字符和尾随不需要的字符来确定。

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);

例如:

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                                                                        |
+------------------------------------------------------------------------------------------------+

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2018-02-16
    • 1970-01-01
    • 2021-07-15
    • 2014-12-28
    • 2014-04-01
    • 2015-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多