Proffesor
Proffesor

Reputation: 2619

SQL search for string within string

I want to create a search function on my site that will return strings within strings. For example: if there was a record in the SQL database where a column's value was "My name is Harry Potter", then the user could search for "Harry P" or "ott" or "My Name", and the record would come up in the search results.

Upvotes: 0

Views: 183

Answers (4)

Radu Murzea
Radu Murzea

Reputation: 10900

If the table is / will be small, then a statement with LIKE '%word%' will be fine.

Although you might be tempted to use a MyISAM table to be able to use a Fulltext index, this is possible in InnoDB too. It will be a bit harder to implement it this way, but it will combine the high-performance text searching that MyISAM offers with all the benefits of InnoDB (transactions, row-level locking etc.).

Upvotes: 1

Fahim Parkar
Fahim Parkar

Reputation: 31627

Use LIKE function.

SELECT * FROM myTable WHERE myName LIKE '%Harry P%'

SELECT * FROM myTable WHERE myName LIKE '%ott%'

SELECT * FROM myTable WHERE myName LIKE '%My Name%'

Here I have assume myName is the field name that you have in your database & myTable is the table name.

In PHP, your query should be

SELECT * FROM myTable WHERE myName LIKE '%$yourWord%'

Upvotes: 1

juergen d
juergen d

Reputation: 204746

select * from your_table
where instr(some_column, 'string_to_search') > 0

Upvotes: 1

mohammad falahat
mohammad falahat

Reputation: 748

use this in your SQL:

SELECT * FROM `table` WHERE title LIKE '%$search_word%'

Upvotes: 2

Related Questions