简体   繁体   中英

Count number of identical rows in MySQL with PHP

I have a table in MySql with a list of keywords. Each keyword was stored in a new row once it was entered by a user on this site.

I have the following query in PHP:

SELECT * FROM keywords GROUP BY query

Which gets all the keywords from MySql, and only shows one of each incase of duplicate keywords. So the output is something like:

Dog
Cat
Lion
Fong

When I'm using $update['query'];

But I'd like to count how many times each keyword appears in the database, so the output would be, for example:

Dog  (2)
Cat  (3)
Lion (1)
Fong (1)

And I'm trying to figure out what the SQL query should be, and how to print it using PHP.

Try this query:

SELECT query, COUNT(1) AS rpt_count FROM keywords GROUP BY query

and in PHP you would access the columns using $update['query'] and $update['rpt_count']

SELECT *, count(*) as cnt FROM keywords GROUP BY query

Use SELECT *, COUNT(*) AS cnt FROM keywords GROUP BY query .

SELECT *, count(1) FROM keywords GROUP BY query
SELECT query, COUNT(query) FROM keywords GROUP BY query
SELECT keyword, COUNT(*) FROM keywords GROUP BY keyword;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM