I'm trying to count all clients that exist in the table policies
Here my tables:
|Policies|
|id| |num_policy| |client_id| |state|
1 1234 1 0
2 5678 1 0
3 9123 1 0
4 4567 2 0
5 8912 3 0
6 3456 4 0
7 7891 4 0
8 2345 4 0
9 6789 4 0
|Clients|
|id| |name|
1 ABC
2 DEF
3 GHI
4 JKL
5 MNO
6 PQR
7 STU
8 VWX
9 YZA
I'm trying to get the count all clients that exist in policies.
|clients| |state|
5 0
I tried: http://sqlfiddle.com/#!2/eb479/2
select count(*) as total from policies p
INNER JOIN clients c ON c.id = p.client_id
WHERE p.state= 0
group by p.client_id
Please somebody can help me?
All can of help will be accepted.
You can just count the distinct client ids that appear in the policies table
select count(distinct client_id) as count
from policies p
inner JOIN clients c ON c.id = p.client_id
WHERE p.state= 0
You do not even need the join
with the client table if you trust your referential integrity, ie if you are sure that all client_id field are filled with valid clients, just do
select count(distinct client_id) as count
from policies p
WHERE p.state= 0
Are you trying to get the clients that have a minimum one policy ?
select p.client_id from policies p
INNER JOIN clients c ON c.id = p.client_id
WHERE p.state= 0
group by p.client_id
Or are you trying to get the count of how many clientes have a policy?
select count(distinct client_id) as count
from policies p
inner JOIN clients c ON c.id = p.client_id
WHERE p.state= 0
Or are you trying to get how many policy each client have?
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.