How can I get the number of times a certain word appears in my sql

How can I get the number of times a certain word occurs 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 appears in my table.

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

      

But he writes a syntax error. NB: I just need the number of times John showed up and I expected him to be four (4).

+3


source to share


4 answers


You can try this partner:

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

      

E: This option is for "John" only.



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

      

E: To display a counter for each .president result in your database. Hooray!

+1


source


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

Use the following syntax:



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

      

PS Don't call your table result

. This is incorrect from the point of view of naming and architecture in general. Name it PresidentsHistory

or PresidentsList

.

+2


source


The syntax is COUNT(`president`)

incorrect.

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

      

+1


source


you can try the following query

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

      

0


source







All Articles