繁体   English   中英

替换 mysql 中的 substring_index

[英]substring_index in replace mysql

可以说我有这样的句子

   id    sentence
    1. I LOVE ORANGE 300G, and i want that Orange 300G
    2. I HATE STRAWBERRY 500G, and i want that Strawberry 500G
    3. i love the orange 300G (with that big), and i want that orange 300G (with that big)
    

我想删除每个句子的最后一个索引,所以预期的结果应该是这样的

I LOVE ORANGE 300G, and i want that Orange
I HATE STRAWBERRY 500G, and i want that Strawberry
i love the orange 300G (with that big), and i want that orange (with that big)

我试过这个

SELECT REPLACE(mytable.sentence, substring_index(mytable.sentence, '', -1), '') AS meta_keywords2 FROM mytable

但由于最后一个索引在 1 个句子中总是与另一个相同,结果是这样的:

I LOVE ORANGE, and i want that Orange
I HATE STRAWBERRY, and i want that Strawberry

这是一个想法:

SELECT SUBSTRING(mytable.sentence, 1, LENGTH(mytable.sentence)-LENGTH(SUBSTRING_INDEX(mytable.sentence, ' ', -1))) AS meta_keywords2 
FROM mytable;

这是一个小提琴

  1. 获取您要提取的句子中最后一个单词的LENGTH()
  2. 获取sentence的总LENGTH()
  3. 在以 position 1开头的sentence上使用SUBSTRING()并提取总长度和最后一个单词长度之间的总字符差。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM