Filipe Aleixo
Filipe Aleixo

Reputation: 4244

Counting the number of pattern occurrences in a certain column with MySQL

I want to search the column Name of a certain table to see how many rows have a Name value that matches a certain pattern. For example, if the pattern I am looking for is %Peter% and in this table there are 5 rows with the Name values:

Peter
Peter Smith
George Peter
Peter Peter
Carl

I want to obtain the value 4. I tried to use COUNT, but don't know how to combine it for example with LIKE. How do I go about to do this?

Upvotes: 0

Views: 117

Answers (1)

Mosty Mostacho
Mosty Mostacho

Reputation: 43434

Not sure why didn't LIKE work for you, but this SHOULD do the trick:

SELECT COUNT(*) FROM table
WHERE name LIKE '%Peter%'

Upvotes: 3

Related Questions