user3264863 user3264863 - 6 months ago 9
MySQL Question

Mysql : I need to search record where searched text is matched with third word

I need to search record where searched text is matched with third word from database record.

Example : Suppose i have three records in description field

This is alex from Usa
This is Michael from India
This is xyz from Canada


I want that what i search should be check in third word only. How is it possible in mysql ? How to write query like that ?

Answer

You can use substring_index

select * from my_table 
where  SUBSTRING_INDEX(SUBSTRING_INDEX(ip,' ',3),' ',-1) =  'Your_text';

or if needed use like

select * from my_table 
where  SUBSTRING_INDEX(SUBSTRING_INDEX(ip,' ',3),' ',-1) like  '%Your_text%';