If you wish to select only the records containing a particular string, e.g., some word, etc., from a MySQL or MariaDB database table then you can use
LIKE
in the SQL SELECT command. E.g., if I have a table named
Books
that contains book information and one of the
columns in the database is Title
, I can select any books from
the database where the title contains "Unix" with the command below:
SELECT * FROM Books WHERE Title LIKE "%Unix%";
The percent sign (%
) serves as a
wildcard character indicating that any number of other charcters will
match it. So by putting the percent sign before and after the word
Unix
, the expression will find any books with titles that have "Unix"
anywhere in the title, e.g., at the beginning, middle, or end of the title.
If I only wanted to find those records in the Books table where the
title began with the word Unix, I could use the command below:
SELECT * FROM Books WHERE Title LIKE "Unix%";
Any characters that came after the word Unix would match, but the title would need to start with "Unix". To find only those books where the word appears at the end of the title, I could use the following command:
SELECT * FROM Books WHERE Title LIKE "%Unix";
If I wanted, instead, to find all of the records where the title doesn't
contain the word "Unix", I could put NOT
before LIKE
.
SELECT * FROM Books WHERE Title NOT LIKE "%Unix%";