开发者

MySQL check how many times a word appears inside a table cell

开发者 https://www.devze.com 2022-12-11 06:46 出处:网络
I want to write a query to select from a table开发者_运维问答 all rows with the word \"piggy\" in a column called Description.

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!


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


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消