1

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.

1
  • At this moment I'd be happy with the simplest solution, but if youre feeling creative I'd love to learn how to do this with/without being case sensitive. Commented Jul 11, 2011 at 21:14

6 Answers 6

3

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']

Sign up to request clarification or add additional context in comments.

Comments

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

2 Comments

Alternatively, use COUNT(*) AS count
@Ryan - Yes an alias should have been there. Updated.
1

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

Comments

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

Comments

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

Comments

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.