Is there a way to select a value which matches partially?
For example, consider:
title 1: The Indiana Highway project
title 2: The Michigan Highway project
Now when the user enters Highway
in the form box both the titles need to be displayed.
If the user enters Michigan
, only one title 开发者_运维问答(title 2) needs to be displayed.
Is there a way to do this in MySQL or PHP?
If you're always searching only for one word, you can use the LIKE
keyword.
SELECT someField, anotherField, FROM table WHERE aField LIKE '%Highway%'
The %
s are wildcards saying that any characters (or none) can be to the left and to the right of Highway.
See documentation here.
If you plan on doing something more sofisticated, take a look at full text search.
Use the LIKE operator in MySQL to select values by partial match.
SELECT title FROM table WHERE title LIKE '%Highway%';
Standard SQL provides LIKE:
WHERE anonymous_column LIKE '%Highway%'
There are other more complicated regular expression operations too, but that would suffice for your immediate purposes.
SELECT * FROM MyTable WHERE title LIKE '%Highway%'
That should do the trick. Something like that will bring back every result where the title has the word Highway somewhere inside
精彩评论