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).
source to share
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!
source to share
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
.
source to share