Damjan Pavlica Damjan Pavlica - 5 months ago 15
SQL Question

SQL query to find all words starting with the exact phrase?

I have many books in the MySQL database. And I am looking for the specific headers. I know how to find every header string that begin with the word "dan":

SELECT * FROM books WHERE header LIKE 'dan%';


But how to find words in the header that begins with phrase "dan"?

Thanks in advance.

Answer

Here's a quick and dirty trick: A word begins with dan can either be at the beginning of a header or after a space, so:

SELECT * FROM books WHERE header LIKE 'dan%' OR header like '% dan%';