Last index of a given substring in MySQL

前端 未结 6 677
余生分开走
余生分开走 2020-12-02 16:27

We can find the index of the first occurrence of a given substring in MySQL using the INSTR() function as follows.

SELECT instr(\'Have_a_good_da         


        
6条回答
  •  醉话见心
    2020-12-02 17:12

    While the above codes work successfully for a single character, they failed when I used them to find the last occurrence of a substring. I therefore recommend the code below for this task:

    SELECT LENGTH("my father is my father")
            - LOCATE('father', REVERSE("my father is my father"))-(LENGTH('father')-1)
    

    This should return 17

提交回复
热议问题