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

BikashKatwal's avatar

SELECT Query for fetching data

I have two tables;

  1. users - id
  2. invoices - user_id, invoice_date(current_date), invoice_number

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.

0 likes
10 replies
BikashKatwal's avatar

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.

willvincent's avatar

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.

BikashKatwal's avatar

@willvincent

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's avatar

Right, as I said, it will select the users not in the invoices table.. as you asked.

BikashKatwal's avatar

@willvincent I am also doing

SELECT id FROM users 
WHERE NOT EXISTS (SELECT user_id FROM invoices WHERE invoice_date=current_date());

but it is not fetching any data. Data type of invoice_date is date.

willvincent's avatar

maybe there aren't any users not in the invoices table for the current date.. in that case no results would be the expected outcome

BikashKatwal's avatar

@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.

sauravs012's avatar
Level 2

Try this, works at my end..

$users = \DB::table("users")->select('*')->whereNotIn('id',function($query) {
    $query->select('id')->from('invoices');
})->get();
willvincent's avatar

In the future you might want to be more specific and clear...

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.

My original answer did that. Fetch data from user table, not in the invoice table, on a specific date.

Please or to participate in this conversation.