Question

I want to write a query to select from a table all rows with the word "piggy" in a column called Description.

SELECT * FROM table WHERE ...?

Thank you!

Was it helpful?

Solution

In MySQL, % is a wild-card. You don't use wild-cards with the = operator but with the LIKE operator.

SELECT * FROM table WHERE `Description` LIKE "%piggy%"

http://dev.mysql.com/doc/refman/5.0/en/string-comparison-functions.html

OTHER TIPS

select * from table where description like '%piggy%'

will select and return all rows where the word piggy is part of the value of the column. If you want to count how many rows then:

select count(*) from table where description like '%piggy%'
select * from table where description like '%piggy%'

As mentioned several times you need a LIKE query for this. I would only warn that this is going to be terribly slow in case of a InnoDB table as it doesn't support fulltext scans. If you want better performance with a LIKE, then you should use MyISAM.

Anyway, if you want to implement a search engine, better look for existing API's. I don't know what programming language you're using, but if it was Java, I'd recommend Apache Lucene.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top