I guess this is more a design pattern question than a SQL one.
In my application a user can join, can create a shop and load products in their shop (so lets say that there is a user, shop (one to one) and products (many to one with the shops).
So I want to be able to do certain things based on time and criteria (but only once) so for example if one week after joining a user has no shop I want to send an email, or 2 weeks after joining with a shop but no products send a email etc – but I only want to do it once.
Its the only running once which is doing my head in – so for example lets say I had this query
select * from user where joineddt < '1 week ago date'
And I had a script which ran every 3 hours, fires off the SQL and then does whatever event (e.g. send a email) I then want to ensure if this script was fired again it would not pick up any users which have already been processed. I thought of introducing a ‘events’ table where once the event for a user has been fired, log a entry for that event, but that again does not seem to work. For example say I had the events table, had id, user, event
select * from user a
left join event b on b.user = a.id
where a.joineddt < '1 week ago date'
and b.event = 'userjoined'
and b.id is null
So when there is no entry in the events able for the ‘userjoined’ event, it will return no results…But really I want it to return the users where there is no event in event table for this ‘userjoined’ event?
So can anyone think of a way to do what I am suggesting?
Your query will always return 0 rows, because if b.id is null, b.event can never be ‘userjoined’.
Move the ‘userjoined’ criteria to the join clause.