简体   繁体   中英

Counting number of rows in mysql once grouped

first time user so sorry if the formatting comes out wrong. not sure how to use this site fully yet.

i'm using php and MySQL.

in my table i have the customers name, address, email, the service they required and the total price

i want to be able to count how many quotes I have for each service, grouping by the email address.

so for instance if jack@john gets 3 quotes for 'oven cleaning', and then 1 quote for 'carpet cleaning', the count will show as 2. at the moment my script is just selecting the email address as the distinct value and not separating the 2 services which is no good as it's throwing off the stats.

Below is what I have for table QUOTES

Email      address           service          total
jack@john  123 fake street   oven cleaning     £100
jack@john  123 fake street   oven cleaning      £85
jack@john  123 fake street   carpet cleaning   £165
ross@joey  new york street   moving cleaning   £300
ross@joey  new york street   moving cleaning   £320
emma@high  the angel street  moving cleaning   £290

$sql="select count(DISTINCT(email)) as total from quotes GROUP BY 'email', 'service' ";
$result=mysqli_query($conSent,$sql);
$data=mysqli_fetch_assoc($result);

return $data['total'];

The above gives me 3 as it's not separating the services, but the answer should be 4.

what am i doing wrong? I don't need any of the rows data returned to me, just the count as one number so that i can say for x month there were x amounts of unique quotes.

thanks

count(DISTINCT(email))

would return the Count of 'UNIQUE' emails regardless of different services. That's why its Total =3, as the grouped rows have only 3 'DISTINCT' emails.

$sql="select email, service from quotes GROUP BY email, service ";
$result=mysqli_query($conSent,$sql);
return mysqli_num_rows($result);

您需要按电子邮件和服务分组,计数服务

SELECT COUNT(DISTINCT email, service) AS total 
FROM quotes 

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM