【问题标题】:If I have two substring_index, which one gets executed first?如果我有两个 substring_index,首先执行哪个?
【发布时间】:2019-09-03 23:02:31
【问题描述】:

如果我有两个 substring_index,首先执行哪一个?内部的substring_index(mycol,'/',3),还是外部的?

substring_index(substring_index(`mycol`,'/',3),'://',-1)

我想治疗的刺痛示例:

https://www.yahoo.com/
http://google.com/en/

我想得到:

www.yahoo.com
google.com

1) 哪个substring_index 先执行?根据首先执行的输出构造第二个substring_index,这一点很重要。

2) 我的substring_index 声明正确吗?

【问题讨论】:

  • 对于每个嵌套函数,总是执行内部的 .. 并重新执行下一个外部的 .

标签: mysql database substring


【解决方案1】:

你可以轻松检查

select substring_index(substring_index('https://www.yahoo.com/','/',3),'://',-1);

www.yahoo.com

select substring_index(substring_index('http://google.com/en/','/',3),'://',-1);

google.com

对于每个嵌套函数,总是执行内部函数 .. 并重新执行下一个外部函数

【讨论】:

    【解决方案2】:

    外部的substring_index() 不能被执行,除非存在一个可以操作的值。
    在这种情况下,要操作的值是:

    substring_index(`mycol`,'/',3)
    

    因此嵌套的substring_index() 会首先执行并提供外部substring_index() 操作的值。
    这是包括 SQL 在内的所有编程语言评估表达式的方式。
    至于第二个问题:是的,该语句是正确的。

    【讨论】:

      【解决方案3】:

      作为使用SUBSTRING_INDEX 的替代方法,如果您使用的是 MySQL 8+,则可以使用正则表达式替换函数:

      SELECT
          REGEXP_REPLACE('http://google.com/en/', 'https?://([^/]+)', '$1') AS url
      FROM yourTable;
      

      Demo

      【讨论】:

        【解决方案4】:
        1. 括号中的语句总是具有更高的优先级。
        2. 内部子字符串索引只是修剪末尾的“/”字符。 ('https://www.yahoo.com')。外部子字符串索引修剪“https://”。 (“www.yahoo.com”)。你的说法刚刚好。

        您可以找到不同的用法或详细信息here

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2022-07-11
          • 1970-01-01
          • 2022-09-23
          • 1970-01-01
          • 1970-01-01
          • 2014-11-29
          • 2016-06-16
          相关资源
          最近更新 更多