开发者

number of months giving wrong results

membership table

  • membership_startdate
  • member_dueday

To find the how many payments(number of months) that the members has paid up to now from membership_startdate and taking into consideration:

SELECT DATEDIFF(GREATEST(NOW(), memberToMship_DueDay), memberToMship_StartDate) AS diff 
  FROM membertomship 

...but it was giving wrong results when we consider memberToMship_StartDate is 2011-07-01 and the mmebership_dueday is 07 The result is 06, but it was wrong.

If memberToMship_StartDate is "2011-01-01" and the due date is 15 then the result is nearly 6 months (jan - jun) (according duedate 15th of month).

I am using mysql

I have tried this

                      SELECT DATE_SUB(DATE_ADD(memberToMship_StartDate,GREATEST(NOW(), memberToMship_DueDay)), INTERVAL memberToMship_DueDay DAY ) AS diff FROM membertomships;

but it was giving error like

                 Error Code: 1064
                         You have an error in your SQL syntax; check the manual that correspond开发者_JS百科s to your MySQL server version for the right syntax to use near 'GREATEST(NOW(), memberToMship_DueDay)), INTERVAL memberToMship_DueDay DAYS ) AS ' at line 1


OK, here's the solution:

SELECT
    PERIOD_DIFF(DATE_FORMAT(now(),'%Y%m'), DATE_FORMAT(membership_StartDate,'%Y%m')) -- month difference
    + (DAY(membership_StartDate) < member_dueday) -- +1 if the start date was before the due day
    + (DAY(now()) > member_dueday) -- +1 if "now" is after the due day
    - 1 -- needed due to the way we are calculating our own adjustments for due day
    as payment_count
from membertomships;

Here's some test data to show the edge cases:

create table membertomships (id int auto_increment primary key, membership_startdate date, member_dueday int);
insert into membertomships values (null, '2011-05-01', 15);
insert into membertomships values (null, '2011-05-30', 15);
insert into membertomships values (null, '2011-05-01', 4);
insert into membertomships values (null, '2011-05-30', 4);
SELECT
    membership_StartDate as start,
    member_dueday as due,
    current_date as today,
    PERIOD_DIFF(DATE_FORMAT(now(),'%Y%m'), DATE_FORMAT(membership_StartDate,'%Y%m')) - 1 + (DAY(membership_StartDate) < member_dueday) + (DAY(now()) > member_dueday) as payment_count
from membertomships;
SELECT id, membership_StartDate as start_date, member_dueday as due_day, current_date as today,
PERIOD_DIFF(DATE_FORMAT(now(),'%Y%m'), DATE_FORMAT(membership_StartDate,'%Y%m')) - 1 + (DAY(membership_StartDate) < member_dueday) + (DAY(now()) > member_dueday) as payment_count
from membertomships;
+----+------------+---------+------------+---------------+
| id | start_date | due_day | today      | payment_count |
+----+------------+---------+------------+---------------+
|  1 | 2011-05-01 |      15 | 2011-07-08 |             2 |
|  2 | 2011-05-30 |      15 | 2011-07-08 |             1 |
|  3 | 2011-05-01 |       4 | 2011-07-08 |             3 |
|  4 | 2011-05-30 |       4 | 2011-07-08 |             2 |
+----+------------+---------+------------+---------------+
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜