tags:

views:

61

answers:

3

Hi,

If it possible to search in a table for records of which its name contains a search term?

Thanks

+1  A: 

Sure. There is the CONTAINS predicate:

... WHERE CONTAINS(name, 'search-term')

There is also the LIKE operator and some DBMS allow for regular expressions.

Joey
+1  A: 
SELECT * FROM `my_table` WHERE name LIKE '%my_search_term%'

or

SELECT * FROM `my_table` WHERE CONTAINS(name, 'search')

But be aware that the LIKE statement is very expensive. If you searching through a lot of text, you might want to consider using Sphinx for exemple.

Mikushi
I had not heard of Sphinx before. Here's a link: http://sphinxsearch.com/
JYelton
Yeah should've linked it. It's very powefull, you populate your Sphinx indexes from MySQL (for example, where i work, we update Sphinx every 2h), and after it's very simple.There is a Sphinx client in PHP: http://php.net/manual/en/book.sphinx.phpVery simple to use, and it's really faster than MySQL for text search.
Mikushi
A: 

It sounds like what you are looking for is LIKE

-- Get all people with phone numbers starting with 920
SELECT * FROM People WHERE PhoneNumber LIKE '920%'
BlueRaja - Danny Pflughoeft