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>

到这里为止,这段代码对我来说很好用

billTable 结构

bill_agents 表结构

当前结果显示 http://i.stack.imgur.com/tMH3P.jpg

我要显示的结果是我想要每日收款报告第 1 天到第 31 天(也应该按金额和支票的总和)它是如何可能的

结果应该是这样 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";
4

2 回答 2

0

一个基本的答案是这样的: -

选择 bill.agent_id、bill.date、bill.amount、bill.cheque、bill_agents.id、bill_agents.name 、SUM(bill.amount)"。"从 bill_agents 左外部加入 bill ON bill.agent_id = bill_agents.id"。 "WHERE YEAR(date) = YEAR(CURDATE()) AND MONTH(date) = 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
于 2013-01-08T09:16:53.930 回答
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
于 2013-01-08T09:22:47.703 回答