Andurit
Andurit

Reputation: 5762

MySQL select with count

In my table of users I have column with name "query" which means how many times user press SEARCH button.

So for example I have:

login | query | company
user1 |   40  |    1
user2 |   60  |    1
user3 |   30  |    2

What I try is to get number of queries of all users in one company. I already tried something like:

$sql = "SELECT count(*) as total_count FROM users WHERE company = :company";

But this just gives me the number of people from one company and I'm not sure how can I edit it to count all queries together.

Upvotes: 1

Views: 55

Answers (2)

Jenson
Jenson

Reputation: 635

Try this code:

$sql = "SELECT sum(query) as total_count FROM users group by company

Upvotes: 1

Mureinik
Mureinik

Reputation: 311308

You could group by the company and sum the number of queries:

SELECT   company, SUM(query) as total_count
FROM     users
GROUP BY company

Upvotes: 4

Related Questions