如何从付款表中查找待处理的客户列表

时间:2014-07-31 07:07:36

标签: php mysql

我在mysql数据库中有3个表,如customers,customeraccount,customerpayment

客户表结构

+----------+------------+---------------+---------------+
| cusid    | cusname    | cusmobileno   | cusaddress    |
+----------+------------+---------------+---------------+
| 1        |  XXXX-A    |    xxxxxxxxxx |   XXXXXX      |
| 2        |  XXXX-B    |    xxxxxxxxxx |   XXXXXX      |
| 3        |  XXXX-C    |    xxxxxxxxxx |   XXXXXX      |
| 4        |  XXXX-D    |    xxxxxxxxxx |   XXXXXX      |
| 5        |  XXXX-E    |    xxxxxxxxxx |   XXXXXX      |
| 6        |  XXXX-F    |    xxxxxxxxxx |   XXXXXX      |
| 7        |  XXXX-G    |    xxxxxxxxxx |   XXXXXX      |
+----------+------------+---------------+---------------+

Customeraccount表结构

    +----------+------------+-----------------+-------------+
    | cusid    | accno      | acc_Create_Date | acc_type    |
    +----------+------------+---------------+---------------+
    | 1        |  0000-1    |    2014-01-05 |        0      |
    | 1        |  0000-2    |    2014-01-15 |        0      |
    | 2        |  0000-3    |    2014-01-15 |        1      |
    | 3        |  0000-4    |    2014-01-15 |        0      |
    | 4        |  0000-5    |    2014-01-16 |        0      |
    | 5        |  0000-6    |    2014-01-15 |        1      |
    | 5        |  0000-7    |    2014-02-05 |        1      |
    | 6        |  0000-8    |    2014-02-07 |        0      |
    +----------+------------+---------------+---------------+

客户付款表结构

        +----------+------------+-----------------+-------------+
        | cpcusid  | cp_accno   | cp_paydate    |   cp_amount   |
        +----------+------------+---------------+---------------+
        | 1        |  0000-1    |    2014-01-05 |        5000   |
        | 1        |  0000-2    |    2014-01-15 |        7000   |
        | 2        |  0000-3    |    2014-01-15 |        4000   |
        | 3        |  0000-4    |    2014-01-15 |        8000   |
        | 4        |  0000-5    |    2014-01-16 |        9000   |
        | 5        |  0000-6    |    2014-01-15 |        6000   |
        | 1        |  0000-1    |    2014-02-05 |        5000   | 
        | 5        |  0000-7    |    2014-02-05 |        9000   |
        | 6        |  0000-8    |    2014-02-07 |        5000   |
        | 1        |  0000-2    |    2014-02-17 |        7000   | 
        | 2        |  0000-3    |    2014-02-15 |        4000   |
        | 3        |  0000-4    |    2014-02-17 |        8000   |
        | 1        |  0000-1    |    2014-03-02 |        5000   |
        +----------+------------+---------------+---------------+

从这些表格中,我需要显示每月等待付款的客户列表,如波纹表(我期待结果) 将从帐户创建日期计算当前日期

    +-------+------------+---------------+-----------+-------------+-----------+
    | CId   | Name       |     Phone     |   Address |   Accnumber | DueMonth  |
    +-------+------------+---------------+-----------+-------------+-----------+
    | 4     |  XXXX-D    |    xxxxxxxxxx | XXXXXXXX  |    0000-5   |  2014/02  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-2   |  2014/03  |
    | 2     |  XXXX-B    |    xxxxxxxxxx | XXXXXXXX  |    0000-3   |  2014/03  |
    | 3     |  XXXX-C    |    xxxxxxxxxx | XXXXXXXX  |    0000-4   |  2014/03  |
    | 4     |  XXXX-D    |    xxxxxxxxxx | XXXXXXXX  |    0000-5   |  2014/03  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-1   |  2014/04  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-2   |  2014/04  |
    | 2     |  XXXX-B    |    xxxxxxxxxx | XXXXXXXX  |    0000-3   |  2014/04  |
    | 3     |  XXXX-C    |    xxxxxxxxxx | XXXXXXXX  |    0000-4   |  2014/04  |
    | 4     |  XXXX-D    |    xxxxxxxxxx | XXXXXXXX  |    0000-5   |  2014/04  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-1   |  2014/05  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-2   |  2014/05  |
    | 2     |  XXXX-B    |    xxxxxxxxxx | XXXXXXXX  |    0000-3   |  2014/05  | 
    | 3     |  XXXX-C    |    xxxxxxxxxx | XXXXXXXX  |    0000-4   |  2014/05  |
    | 4     |  XXXX-D    |    xxxxxxxxxx | XXXXXXXX  |    0000-5   |  2014/05  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-1   |  2014/06  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-2   |  2014/06  |
    | 2     |  XXXX-B    |    xxxxxxxxxx | XXXXXXXX  |    0000-3   |  2014/06  |
    | 3     |  XXXX-C    |    xxxxxxxxxx | XXXXXXXX  |    0000-4   |  2014/06  |
    | 4     |  XXXX-D    |    xxxxxxxxxx | XXXXXXXX  |    0000-5   |  2014/06  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-1   |  2014/07  |
    | 1     |  XXXX-A    |    xxxxxxxxxx | XXXXXXXX  |    0000-2   |  2014/07  |
    | 2     |  XXXX-B    |    xxxxxxxxxx | XXXXXXXX  |    0000-3   |  2014/07  |
    | 3     |  XXXX-C    |    xxxxxxxxxx | XXXXXXXX  |    0000-4   |  2014/07  |
    | 4     |  XXXX-D    |    xxxxxxxxxx | XXXXXXXX  |    0000-5   |  2014/07  |
    +-------+------------+---------------+-----------+-------------+-----------+

付款将按月支付计划,因此日期应来自customeraccount表acc_create_date字段。 例如,如果客户帐户是在2014-06-25创建的,那么如果我在2014-07-24上看到该报告,则不应显示它是待处理的付款,如果我在2014-07-26看到该报告,则应显示为待付款。

请任何人帮助我获得上述结果。

1 个答案:

答案 0 :(得分:0)

试试这个PHP + MYSQLi代码,(MySQL查询经过测试但不是PHP - 你可以自己做)

$result=mysqli_query($dbCon,"SELECT min(date) FROM customeraccount;");
if($row=mysqli_fetch_array($result)){ $minDate=date_create(row[0]); }

$currDate=date_create();

while($currDate>date_add($minDate,date_interval_create_from_date_string('1 month'))){
      $result=mysqli_query($dbCon,"select ca.cusid,ca.accno,c.cusname,c.cusmobileno,c.cusaddress from customerpayment cp right join customeraccount ca on ca.accno=cp.cp_accno and year('$minDate')=year(cp.cp_paydate) and month('$minDate')=month(cp.cp_paydate) left join customers c on ca.cusid=c.cusid where month('$minDate')>=month(ca.acc_create_date) and year('$minDate')>=year(ca.acc_create_date) and cp.cpcusid is null;");
      echo "<table>";
      while($row=mysqli_fetch_array($result)){
           echo "<tr><td>".join($row,"</td><td>")."</td><td>".date_format($minDate,"Y/m")."</td></tr>";
      }
      echo "</table>";
}