Dumb_Shock
Dumb_Shock

Reputation: 1008

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?

Upvotes: 2

Views: 62

Answers (4)

Tony318
Tony318

Reputation: 562

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

Upvotes: 2

Jonathan Leffler
Jonathan Leffler

Reputation: 754590

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.

Upvotes: 2

user470714
user470714

Reputation: 2888

Use the LIKE operator in MySQL to select values by partial match.

SELECT title FROM table WHERE title LIKE '%Highway%';

Upvotes: 2

dee-see
dee-see

Reputation: 24078

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.

Upvotes: 4

Related Questions