Kizzim
Kizzim

Reputation: 112

mysql - count two columns in same query with specific variables

I have a mysql database structured like:

table transactions:

id  paymentid clientid status
------------------------------
1   001       12345      0
2   002       11223      1
3   003       12345      4
4   004       12345      4

What is the most efficient way to run a query in mysql to give me the number of times a certain clientid had payments with the status of 4?

like:

select clientid, count(*) 
where status = 4 

but only return the count of a specific clientid.

Upvotes: 0

Views: 124

Answers (1)

Taryn
Taryn

Reputation: 247710

You will just add a WHERE filter for clientId if you want to filter by a specific client. If not, then you can remove the and clientid = 12345:

select clientid, count(*) 
from transactions
where status = 4 
  and clientid = 12345
GROUP BY clientId;

See SQL Fiddle with Demo

Upvotes: 1

Related Questions