0

I have a column named 'titles' with the values:

1) Harry Potter

2) Harry

3) Harry Po

4) Mario Potter

5) Hanry Plotter

If i make a search like:

SELECT * FROM books WHERE titles LIKE 'Harry Potter%'

i get only the first row with 'Harry Potter' which is ok. But how can i get the 2nd and the 3rd row also as a result because they match the search string 'Harry Potter' (Harry Potter contains Harry, Harry Potter contains Harry Po). BUT not "Harry Potter" Not contains "Mario Potter"

I am looking for a function like: if search_string contains ROW

Thanks

Mike Aron
  • 550
  • 5
  • 13
  • You have to define the rule a bit better.. Should Potter, Harry be returned? Or Harry, the boy who lived, Potter? – kjmerf May 15 '18 at 01:17
  • If i search for "Harry Potter" all the rows which are a part of and beginning "Harry Potter" should returned like "Ha", "Harry", "Harry Po". BUT NOT "Potter". I am editing the question. Thanks – Mike Aron May 15 '18 at 01:25

1 Answers1

2

Use LIKE with your column surrounded by wildcards:

SELECT *
FROM books
WHERE 'Harry Potter' LIKE CONCAT('%', titles, '%')

If you have a long term need for such searches, you might want to look into using MySQL's full text search capabilities.

Tim Biegeleisen
  • 502,043
  • 27
  • 286
  • 360