Search code examples
mysqlsqlsearchsubstringsql-like

SQL - Find all words that start with some phrase


I have many books in my MySQL database. And I am looking for a specific words in my book headers. For example, I can find every header begins with the word "dan":

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

But how to find a word inside the header that begins with the certain phrase?

Thanks in advance.


Solution

  • 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%';