【问题标题】:How to find list of customers pending dues from their payment table如何从他们的付款表中找到客户待付款的列表
【发布时间】:2014-07-31 07:07:36
【问题描述】:

我在 mysql 数据库中有 3 个表,例如客户、客户帐户、客户付款

客户表结构

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

客户账户表结构

    +----------+------------+-----------------+-------------+
    | 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 年 6 月 25 日创建的,那么如果我在 2014 年 7 月 24 日看到报告,它不应该显示它是待付款,如果我在 2014 年 7 月 26 日看到报告,它应该显示为待付款。

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

【问题讨论】:

  • 那你有什么尝试?
  • 是客户表cusid的参考
  • 我试过这样。它会给出会费总数的准确结果。 SELECT cus_id,cus_firstname,cus_mobile,cus_address,ca_cusaccnumber, sum(((SELECT TIMESTAMPDIFF(MONTH, ca_createdtime, DATE_SUB(NOW(), INTERVAL 1 DAY)))+1) - (SELECT count(*) FROM chf_customerpayments WHERE cp_cusaccno = ca_cusaccnumber) )) as previousdues FROM chf_customeraccount LEFT JOIN chf_customers ON cus_id = ca_cusaccid WHERE ca_accountstatus=1 AND ca_cusaccounttype=0 AND TIMESTAMPDIFF(MONTH, ca_createdtime, DATE_SUB(NOW(), INTERVAL 1 DAY)) > 0 Group by ca_cusaccnumber
  • 所以客户可以互相支付对方的账户?
  • 不,他们只能为自己的帐户付款。客户可以拥有多个帐户。他们将在到期日的每个月为其多个帐户付款。

标签: php mysql


【解决方案1】:

试试这个 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>";
}

【讨论】:

    猜你喜欢
    • 2015-09-23
    • 2021-08-13
    • 2012-08-16
    • 2021-12-28
    • 2015-03-01
    • 2013-08-15
    • 1970-01-01
    • 2012-09-29
    • 2013-05-26
    相关资源
    最近更新 更多