Pet'r
Pet'r

Reputation: 33

how can i get the number of times a specific word occur in my sql

Please how can I get the number of times, a specific word occur in a table? I have a table called result and a field called president.

Index - president  
1 - James  
2 - John  
3 - John  
4 - John  
5 - James  
6 - John 

I tried using the following codes to get the number of times John occur in my table.

SELECT COUNT(`president`) FROM `result` WHERE `president`="John"

But, it's writing syntax error. N.B: What I just need is the number of times John appeared which I expected to be four(4).

Upvotes: 3

Views: 60

Answers (4)

Zymon Castaneda
Zymon Castaneda

Reputation: 759

You can try this mate:

SELECT
    president, COUNT(index) 'occured'
FROM
    result
WHERE
    president = 'John';  

E: This one is specific for 'John' only.

SELECT
    president, COUNT(index) 'occured'
FROM
    result
GROUP BY
    president;  

E: To display the count for each result.president in your database. Cheers!

Upvotes: 0

tning
tning

Reputation: 1251

Syntax COUNT(`president`) is not correct.

SELECT COUNT(*) FROM `result` WHERE `president` = "John"

Upvotes: 1

sudhansu63
sudhansu63

Reputation: 6180

you can try the below query

SELECT COUNT(*) FROM `result` WHERE `president` LIKE "John"

Upvotes: 0

Yeldar Kurmangaliyev
Yeldar Kurmangaliyev

Reputation: 34189

You don't need to use COUNT on a column. In your case, you want to get
the number of rows where the president is 'John'.

Use the following syntax:

SELECT COUNT(*) FROM `result` WHERE `president` = "John"

P.S. Don't call your table result. It is kind of incorrect in terms of naming and architecture in general. Call it PresidentsHistory or PresidentsList.

Upvotes: 2

Related Questions