views:

45

answers:

2

I have a table of customers with a 1 recorded against their customerid on different dates. I would like to find the sum of the 1's recorded in descending order. I'm using MySQL and php Thanks

A: 

As i get you right, this is simple sql request what u need:

SELECT COUNT(id) as total from customers

Just make in php:

   $sql="SELECT COUNT(id) from customers";
    $query=mysql_query($sql) or die(mysql_error());
      $res=mysql_fetch_assoc($query);

 $summ=$res['total'];   //<- Your summ (i.e. quantity of rows in table)

Btw, you can use mysql_num_rows instead.

Or explain please more accurately what output you need. To make sorting by date or any other dependency you will need to make other request using WHERE clause and date comparison.

moogeek
Sorry newbie error even though I've read this board many times I've avoided asking questions cause I knew I wouldnt explain it properly. I want to list the customers with the most number of 1's recorded, say the top 10 ie customer 46 has 10 1's, customer 36 has 9 1's, etc
bsandrabr
thanks and thanks for the detail of including the php as well
bsandrabr
A: 

My guess is that you want the sum of records marked with 1 per customer and sort that result in descending order? If so, the following should do the trick :

select cust.id, sum(cone.one) as number_ones 
from customers as cust
inner join customer_ones as cone on cone.id=cust.id
group by cust.id
order by number_ones desc

This is assuming that 'one' is the column containing ones (and only contains 0 or 1 - otherwise you will have to add WHERE cone.one = 1), customers is your customer table and customer_ones is the table containing your customer data.

wimvds