and I have also set up a cron job that calls the query every hour.
I have to write a query/stored procedure that fetches only the data from the users table that doesn't exist in the invoices table on that day.
Eg:
If user_id 1 exists on invoices table on 2/19/2020 then, then user_id 1 should not be fetched on that day.
but the next day it should fetch the user id, because the current date is not equal to invoice_date of user_id 1.
Concept: The concept of the job is to send invoices every day but once the email is sent on that day, it should not sent the same email for the same user on the same day. But it should send the email to the same user the next day.
As it happens, I wrote a query using similar logic to what is necessary for this today..
You'll want to run something like this:
SELECT id FROM users
WHERE NOT EXISTS (
SELECT * FROM invoices
WHERE invoice_date = ?
)
Where that placeholder is the current date.. you may need to do some date casting or include a > and < range to capture a full day if the field is actually a datetime not just a date, but the where not exists is what you want to determine what from the full list of users does not exist in the invoices table.
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,
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.expires_on=DATE_ADD(CURRENT_DATE(), INTERVAL 1 MONTH)
AND u.activated=1 AND u.subscription='yearly' group by u.id;
END;
-- CALL `get_users_halls`;
this selects the users but I want to select the data that does not exist in the invoices table. Could you please suggest me how can I write the query in this situation.
@willvincent There are many other users in users table, Just 5 users are in invoices table. Basically it should select all other users excepts the one in invoice table.