Loop PHP, NO DUPLICATE Field from by group, sum mysql

Rikudo Pain

this is my table 'dp'

Year      | Month     | Payment|  Payer_ID | Payment_Recipient |
2008/2009 | July      | 100000 |     1     |        John       |
2008/2009 | August    | 200000 |     1     |        Jane       |
2009/2010 | August    | 150000 |     1     |        Jane       |
2009/2010 | September | 175000 |     1     |        John       |

I need output like this in my php page :

Year      |       July     |      August   |     September | 
2008/2009 | 100000 | John  | 200000 | Jane |    0   |   -  | 
2009/2010 |    0   |   -   | 150000 | Jane | 175000 | John |

I use this sql query :

$query_sql = "SELECT
   year
 , SUM(IF(Month='July', Payment, 0)) As 'July'
 , SUM(IF(Month='August', Payment, 0)) As 'August'
 , SUM(IF(Month='September', Payment, 0)) As 'September'
FROM 
 dp
WHERE 
 Payer_ID = 1
GROUP BY
 Year";


<table>
<tr>
<td>year</td>
<td>July</td>
<td>August</td>
<td>September</td>
</tr>
<?php
while($data=mysql_fetch_array($sql)){
?>
<tr>
<td><?php echo $data['year']; ?></td>
<td><?php echo $data['July']; ?></td>
<td><?php echo $data['August']; ?></td>
<td><?php echo $data['September']; ?></td>
</tr>
<?php } ?>
</table>

and the output always like this :

Year      |  July  | August | September | 
2009/2010 |    0   | 150000 | 175000    |

only show 1 year (not looping).

Can somebody help me to fix my code in 'sum(if' to include payment recipient and fix my variable in php to loop?

Kickstart

Crude SQL way of doing it:-

   SELECT DISTINCT dp.Year, SubJuly.Payment, SubJuly.Payment_Recipient, SubAugust.Payment, SubAugust.Payment_Recipient, SubSeptember.Payment, SubSeptember.Payment_Recipient
    FROM dp
    LEFT OUTER JOIN
    ( 
        SELECT Year, Month, Payment, Payer_ID, Payment_Recipient 
        FROM dp
        WHERE Month = 'July'
    ) SubJuly
    ON dp.Year = SubJuly.Year
    LEFT OUTER JOIN
    ( 
        SELECT Year, Month, Payment, Payer_ID, Payment_Recipient 
        FROM dp
        WHERE Month = 'August'
    ) SubAugust
    ON dp.Year = SubAugust.Year
    LEFT OUTER JOIN
    ( 
        SELECT Year, Month, Payment, Payer_ID, Payment_Recipient 
        FROM dp
        WHERE Month = 'September'
    ) SubSeptember
    ON dp.Year = SubSeptember.Year
WHERE  dp.Payer_ID = 1

It could probably done more efficiently by using a list of months / years and LEFT JOINing that against the original table, then just working on the change of row in the php.

Lobbing that back into your original php:-

<?php


$query_sql = "   SELECT DISTINCT dp.Year, SubJuly.Payment, SubJuly.Payment_Recipient, SubAugust.Payment, SubAugust.Payment_Recipient, SubSeptember.Payment, SubSeptember.Payment_Recipient
    FROM dp
    LEFT OUTER JOIN
    ( 
        SELECT Year, Month, Payment, Payer_ID, Payment_Recipient 
        FROM dp
        WHERE Month = 'July'
    ) SubJuly
    ON dp.Year = SubJuly.Year
    LEFT OUTER JOIN
    ( 
        SELECT Year, Month, Payment, Payer_ID, Payment_Recipient 
        FROM dp
        WHERE Month = 'August'
    ) SubAugust
    ON dp.Year = SubAugust.Year
    LEFT OUTER JOIN
    ( 
        SELECT Year, Month, Payment, Payer_ID, Payment_Recipient 
        FROM dp
        WHERE Month = 'September'
    ) SubSeptember
    ON dp.Year = SubSeptember.Year
WHERE  dp.Payer_ID = 1";

....................
?>
<table>
<tr>
<td>year</td>
<td colspan='2'>July</td>
<td colspan='2'>August</td>
<td colspan='2'>September</td>
</tr>
<?php
while($data=mysql_fetch_assoc($sql))
{
    echo "<tr><td>".implode("</td><td>", $data)."</td></tr>";
} 
?>
</table>

Note you probably should be using mysqli_* functions rather than the mysql_* functions.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Group and sum duplicate elements in a for loop

From Dev

MySQL SUM by field an then GROUP BY user?

From Dev

MySQL SUM and GROUP BY from AS column

From Dev

select (sum(field) - max(field)) from table group by field

From Dev

MySQL SUM over a virtual subquery field (with another SUM) with GROUP BY

From Dev

Duplicate the group by field value

From Dev

php mysql calculate, sum and group by given parameters

From Dev

Remove duplicate data from loop php

From Dev

Mysql SUM and GROUP BY from 3 tables

From Dev

MYSQL sum from two different tables group by

From Dev

MySQL Count and SUM from second table with group by

From Dev

Select rows from table that have duplicate field, but prioritize group by

From Dev

Prevent duplicate records from submitting; PHP & MySQL

From Dev

How To Find Duplicate, Count and Sum Values from different columns in MySQL?

From Dev

MySQL select and sum from multiple table without duplicate it

From Dev

PHP LOOP from database and save into one field

From Dev

Remove Duplicate record from Mysql Table using Group By

From Dev

Validate against duplicate field from database in oop php

From Dev

Retrieve sum of two different table column field in mysql using php

From Dev

Retrieve sum of two different table column field in mysql using php

From Dev

Php, MySql Sum from two tables

From Dev

PHP MySQL query (sum from different rows)

From Dev

Cumulative sum in PHP Array from MySQL Query

From Java

Duplicate positions from group

From Dev

Group by a column from csv and sum the values from another (PHP)

From Dev

MySQL ranking with GROUP BY and SUM

From Dev

MYSQL: SUM with multiple group

From Dev

sum in mysql than group by

From Dev

MYSQL: SUM with multiple group

Related Related

  1. 1

    Group and sum duplicate elements in a for loop

  2. 2

    MySQL SUM by field an then GROUP BY user?

  3. 3

    MySQL SUM and GROUP BY from AS column

  4. 4

    select (sum(field) - max(field)) from table group by field

  5. 5

    MySQL SUM over a virtual subquery field (with another SUM) with GROUP BY

  6. 6

    Duplicate the group by field value

  7. 7

    php mysql calculate, sum and group by given parameters

  8. 8

    Remove duplicate data from loop php

  9. 9

    Mysql SUM and GROUP BY from 3 tables

  10. 10

    MYSQL sum from two different tables group by

  11. 11

    MySQL Count and SUM from second table with group by

  12. 12

    Select rows from table that have duplicate field, but prioritize group by

  13. 13

    Prevent duplicate records from submitting; PHP & MySQL

  14. 14

    How To Find Duplicate, Count and Sum Values from different columns in MySQL?

  15. 15

    MySQL select and sum from multiple table without duplicate it

  16. 16

    PHP LOOP from database and save into one field

  17. 17

    Remove Duplicate record from Mysql Table using Group By

  18. 18

    Validate against duplicate field from database in oop php

  19. 19

    Retrieve sum of two different table column field in mysql using php

  20. 20

    Retrieve sum of two different table column field in mysql using php

  21. 21

    Php, MySql Sum from two tables

  22. 22

    PHP MySQL query (sum from different rows)

  23. 23

    Cumulative sum in PHP Array from MySQL Query

  24. 24

    Duplicate positions from group

  25. 25

    Group by a column from csv and sum the values from another (PHP)

  26. 26

    MySQL ranking with GROUP BY and SUM

  27. 27

    MYSQL: SUM with multiple group

  28. 28

    sum in mysql than group by

  29. 29

    MYSQL: SUM with multiple group

HotTag

Archive