首页 文章

如何从2日起获得月数和天数的所有月份?

提问于
浏览
0

我有2张 table :

a)客户

clients_id    conn_date
=======================
1             2016-06-01     
2             2016-07-17
3             2016-06-22
4             2016-09-03

b)clients_pay_bill

cpp_id     clients_id    paid_month
===================================
1          1             2016-07-03 
2          2             2016-07-22
3          4             2016-09-09             
4          2             2016-07-22

现在我想显示所有月 s ,其中包含直到当前日期才支付客户的天数和月数 .

例如 :

clients_id = 1 连接日期(conn_date)是 2016-06-01 ,他现在只支付 2016-07-03 月账单 . 所以sql查询将在几个月后输出:

2016-06
2016-08
2016-09
**2016-07 will not print because he already paid this months bill**

而且我也希望显示天数和月数,例如:某些天的3个月......

我无法想象sql查询应该如何?

1 回答

  • -1

    通过执行获取客户端的 conn_date 以及 paid_month

    SELECT conn_date FROM clients WHERE client_id = <your_client_id>
    

    SELECT paid_month FROM clients_pay_bill WHERE clients_id = <your_client_id>
    

    然后你可以使用PHP来获取从conn_date到current_date的所有日期的数组 .

    然后foreach日期检查它是否存在于paid_month数组中 . 如果没有,则显示 .

    这是 pseudo code

    $start    = (new DateTime(conn_date));
    $end      = date(Y-m-d);
    $interval = DateInterval::createFromDateString('1 month');
    $period   = new DatePeriod($start, $interval, $end);
    
    foreach ($period as $dt) {
        if(!in_array($dt,$paidMonthArray)) {
             display($dt(Y-m));
        }
    }
    

相关问题