2013-01-08 342 views
0
<?php 
    $query4 = "SELECT bill.agent_id, bill.date, bill.amount, bill.cheque, bill_agents.id,bill_agents.name ". 
    "FROM bill, bill_agents ". 
    "WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ". 
    "ORDER BY bill.date ASC"; 

    $result4 = mysql_query($query4) or die('Error, query failed1'); 
    if (mysql_num_rows($result4) > 0) { 
     mysql_data_seek( $result4, 0); 
?> 

<?php while($row4 = mysql_fetch_array($result4, MYSQL_ASSOC)) { ?> 

    <tr> 
    <td><?php echo $row4['name']; ?></td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    <td>&nbsp;</td> 
    </tr> 
    <?php } } ?> 
</table> 

到這裏該代碼工作正常,我Php Mysql每日結果循環?

billTable結構

bill_agents表結構

當前結果顯示 http://i.stack.imgur.com/tMH3P.jpg

我想顯示在結果我想每天收集報告日復一日1至31天(並且還應該通過量的總和,並檢查),它是如何possble

結果應該是像這 http://i.stack.imgur.com/S08or.jpg

此外,我在使用"GROUP BY bill.agent_id".在上面使用相同的SQL和它粘貼下面時出現錯誤。

$query4 = "SELECT bill.agent_id, bill.date, bill.amount, bill.cheque, bill_agents.id,bill_agents.name ". 
    "FROM bill, bill_agents ". 
    "WHERE bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) ". 
    "GROUP BY bill.agent_id". 
    "ORDER BY bill.date ASC"; 
+0

當前結果顯示http://i.stack.imgur.com/tMH3P.jpg結果應該是這樣的http://i.stack.imgur.com/S08or.jpg – user1954950

+1

你需要經過空間'「GROUP BY bill.agent_id」'。 –

+0

@Teodor Blazhev你是對的,以及如何可以每天reulst – user1954950

回答

0

請嘗試以下查詢。 GROUP_CONCAT會給你支票號碼列表和sum of all the check amount that belongs to one agent

SELECT 
     bill.agent_id, 
     bill.date, 
     SUM(bill.amount), 
     GROUP_CONCAT(bill.cheque), 
     bill_agents.id, 
     bill_agents.name 
    FROM 
     bill, 
     bill_agents 
    WHERE 
     bill.agent_id = bill_agents.id AND YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = MONTH(CURDATE()) 
    GROUP BY bill.agent_id 
    ORDER BY bill.date ASC 
0

一個基本的答案是這樣的: -

SELECT bill.agent_id,bill.date,bill.amount,bill.cheque,bill_agents.id,bill_agents.name,SUM(bill.amount )」。 「FROM bill_agents LEFT OUTER JOIN紙幣ON bill.agent_id = bill_agents.id」。 「WHERE YEAR(日期)= YEAR(CURDATE())和月(日期)= MONTH(CURDATE())」。
「GROUP BY bill.agent_id」。 「ORDER BY bill.date ASC

然而,最大的問題是有很多天代理人沒有活動。當你需要的是一個表格來生成月份的日期(這可能只是選擇一個月的第一天的數字範圍),你交叉參加代理人,以獲得代理和本月的一天。你離開了那張賬單。

更多的戲劇。沒有測試過,所以可能有些拼寫錯誤,不太清楚你想要的數字,但希望能給你提供一些想法。

SELECT bill.agent_id, MonthDays.DayOfMonth, bill.date, bill.amount, bill.cheque, bill_agents.id,bill_agents.name , SUM(bill.amount) 
FROM bill_agents 
CROSS JOIN (SELECT a.i+b.i*10 AS DaysToAdd, LAST_DAY(NOW()) - (a.i+b.i*10) AS DayOfMonth 
FROM integers a, integers b 
HAVING DaysToAdd < DAY(LAST_DAY(NOW()))) MonthDays 
LEFT OUTER JOIN bill ON bill.agent_id = bill_agents.id AND bill.date = MonthDays.DayOfMonth 
GROUP BY bill.agent_id 
ORDER BY bill.date ASC, MonthDays.DayOfMonth