mysql - How can sum using group by? -


i'm trying count clients exist in table policies

here 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 count clients exist in policies.

   |clients|  |state|       5          0 

i tried: http://sqlfiddle.com/#!2/eb479/2

select count(*) total policies p inner join clients c on c.id = p.client_id p.state= 0 group p.client_id 

please can me?

all can of accepted.

you can count distinct client ids appear in policies table

select count(distinct client_id) count  policies p   inner join clients c on c.id = p.client_id p.state= 0 

you not need join client table if trust referential integrity, i.e. if sure client_id field filled valid clients, do

select count(distinct client_id) count  policies p p.state= 0 

Comments

Popular posts from this blog

javascript - jquery or ashx not working -

opencv - DataType<cv::detail::deriv_type>::depth what is it used for -

python 3.x - Mapping specific letters onto a list of words -