循环PHP,没有DUPLICATE字段来自group,sum mysql

时间:2013-11-08 11:57:42

标签: php mysql loops

这是我的表'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       |

我需要在我的php页面中输出这样的内容:

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

我使用这个SQL查询:

$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>

,输出总是这样:

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

仅显示1年(不循环)。

有人可以帮我修复我的代码'总和(如果'包括付款收件人并修复我的变量在php中循环?

2 个答案:

答案 0 :(得分:2)

粗俗的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

通过使用一个月/年的列表和LEFT JOINing对原始表,然后只是在php中改变行,它可能更有效率。

将其丢回原来的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>

请注意,您可能应该使用mysqli_ *函数而不是mysql_ *函数。

答案 1 :(得分:0)

您的代码是正确的..我认为您使用

 mysql_fetch_array

while while循环之前的功能。检查你的代码