Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

BikashKatwal's avatar

SELECT Query to get all the data of 7 days

DROP PROCEDURE IF EXISTS `get_users_halls`;
delimiter ;;
CREATE PROCEDURE `get_users_halls` ()
BEGIN
  
SELECT u.id , u.email,
    u.first_name,
    u.last_name,
    count(*) as total_halls,
    u.override_membership_price,
    CASE
        WHEN u.override_membership_price IS NULL THEN (((count(*)-1) * 50)+100)*0.1
            ELSE  u.override_membership_price * 0.1
    END AS gst_amount,
    CASE
        WHEN u.override_membership_price IS NULL THEN ((count(*)-1) * 50)+100
            ELSE u.override_membership_price
    END AS total_amount,
    u.subscription,
    u.expires_on,
    DATE_ADD(u.expires_on, INTERVAL 1 YEAR) as extended_date,
     CASE
        WHEN u.override_membership_price IS NULL THEN TRUE
            ELSE FALSE
        END AS display_info
        FROM users u INNER JOIn halls h ON u.id=h.user_id
WHERE u.id NOT IN (SELECT i.user_id FROM invoices i WHERE i.invoice_date=CURRENT_DATE()) 
AND u.expires_on = DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH)
AND u.activated=1 AND u.subscription='yearly' group by u.id;
END;

The above gives the data whose invoice date is expiring 1 month from today's date. I have CRON job running the script, but I am also thinking if in any cases Job didn't run then all the invoice that needs to be generated within that time also need to be sent for the users. So I am planning to fetch the 7 days ahead data as well. So that all the users that have been missed also get the invoice including all the users whose date is expiring after 1 month(which the above scripts does), but I am also thinking to get the missing data. How can I do that any logic to do the task would be appreciated.

0 likes
2 replies
alanamadeus's avatar

I am (ahmkml36@gmail.com) looking to hire 6 great PHP programmers (full time works from home) so I interviewed smart female developer and I asked her to solve this task and this was her reply:

Solution One:

FROM users WHERE u.id NOT IN (SELECT i.user_id FROM invoices i WHERE i.invoice_date=CURRENT_DATE()) // This line was added to solve the issue. AND u.expires_on between DATE_ADD(DATE_SUB(current_date(), INTERVAL 7 DAY), INTERVAL 1 MONTH) and DATE_ADD(current_date(), INTERVAL 1 MONTH) AND u.activated=1 AND u.subscription='yearly' group by u.id

Solution Two: I found another solution to this problem. I found many ways of managing cronJobs and when jobs are misfired then there should be a recovery process, so it is possible to have Cron to solve the problem by itself.

cheers

BikashKatwal's avatar

Thanks, @alanamadeus I have used the first approaches to fix the issue.

 u.expires_on BETWEEN DATE_SUB(DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH), INTERVAL 1 WEEK) 
AND DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH)

Please or to participate in this conversation.