Jump to content

find how many there are??


snowman2344

Recommended Posts

I have a field in mysql named PAYMENT as follows

 

PAYMENT

25

15

10

10

10

25

25

25

15

 

I need to count how many of each payment types there are and add them up

 

EG output would be

 

3 people paid $10 for a total of $30

2 people paid $15 for a total of $30

4 people paid $25 for a total of $100

 

The total revenue for the show is $160

 

Thanks

Link to comment
Share on other sites

this is something i came up with quick, not tested

 

$amount = 0;
$number = 0;
$revenue = 0;

$search = "SELECT PAYMENT FROM table ORDER BY PAYMENT ASC";
$result = mysql_query($search) or die ("SQL Error:" . mysql_error());
while ($row = mysql_fetch_array($result))
{
  if ($amount == $row['PAYMENT'])
  {
    $number += 1;
  }
  else
  {
    if($number > 0)
    {
      $total = $number * $amount;
      $revenue += $total;
      echo $number ." people paid $". $amount ." for a total of $". $total ."<br />";
      $number = 1;
      $amount = $row['PAYMENT'];
    }
  }
}
echo "<br />The total revenue for the show is $". $revenue;

Link to comment
Share on other sites

that's a very intensive way of doing it when you can use MySQL's built-in functions. try something like this:

 

SELECT COUNT(*) AS total, payment AS payment_amount FROM table GROUP BY payment ORDER BY payment

 

the total amount of money paid for each amount is simply the total multiplied by the payment amount. you could even put it into the query:

 

SELECT COUNT(*) AS total_times, SUM(payment) AS total_paid, payment AS payment_amount FROM table GROUP BY payment ORDER BY payment

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.