MYSQL查询-获取倒数第二个分隔值

MYSQL查询-获取倒数第二个分隔值,mysql,Mysql,我的记录集有一个包含以下值的字段: Product Tree 1->Product Tree 2->Product Tree 3->Product Tree 4 Product Tree 1->Product Tree 2->Product Tree 3 Product Tree 1->Product Tree 2-> 我想使用'->'作为delimeter返回倒数第二个分隔值。 因此,在我的示例中,第一行将返回“产品树3”,第二行返回“产品树2”,第

我的记录集有一个包含以下值的字段:

Product Tree 1->Product Tree 2->Product Tree 3->Product Tree 4
Product Tree 1->Product Tree 2->Product Tree 3
Product Tree 1->Product Tree 2->
我想使用'->'作为delimeter返回倒数第二个分隔值。 因此,在我的示例中,第一行将返回“产品树3”,第二行返回“产品树2”,第三行返回“产品树1”。我不能将PHP用于此目的,它必须在MYSQL中完成

我开始尝试使用子字符串索引。 我发现:

SELECT REPLACE( SUBSTRING_INDEX( 'Product Tree 1->Product Tree 2->Product Tree 3->Product Tree 4', '->', 3 ) , 'Product Tree 1->', '' ) AS header
返回“Product Tree 2->Product Tree 3”,因此在第一个“Product Tree 1”之后给出下一个分隔的2个值


但是我该如何获取倒数第二个分隔值呢?

一种奇怪的格式,用于将数据保存在关系数据库中。假设您可以使用并执行以下操作:

SET @foo = 'Product Tree 1->Product Tree 2->Product Tree 3->Product Tree 4';

SELECT substring_index(substring_index(@foo, '->', -2), '->', 1);