Reputation: 1754
Suppose I have 2 tables
Table 1: users
Attributes: id (int), expires_at (varchar), status (enum)
expires_at could be a DATETIME, or it could be a number (the number of logins the person can perform).
status can be "active", "expiring", "expired", etc..
Table 2: staff
Attributes: id (int), user_id (int), contract (enum)
contract can be either: "chronologically" or "fixed number of times"
If "chronologically", users's expires_at will have a DATETIME value.
If "fixed number of times", users's expires_at will have a number value as VARCHAR.
One approach could be to create a scheduled event on MySQL so that, every day, at 11:59am, MySQL will check expires_at and set status as expiring or expired.
I'm looking for something like:
UPDATE users SET status = CASE (SELECT expires_at FROM users WHERE status = "active" AND id IN (SELECT id FROM staff WHERE contract = "fixed number of times")
WHEN expires_at < 10 AND expires_at > 0 THEN "expiring"
WHEN expires_at <= 0 THEN "expired"
END
for "fixed number of times" contract, and another similar query for "chronologically". Problem is: the above query's syntax is wrong.
Upvotes: 0
Views: 75
Reputation: 3774
i believe u should use
SELECT user_id FROM staff
not
SELECT id FROM staff
try
UPDATE users
SET
status =
CASE
WHEN expires_at < 10 AND expires_at > 0 THEN 'expiring'
WHEN expires_at <= 0 THEN 'expired'
ELSE status
END
WHERE
id IN (SELECT user_id FROM staff WHERE contract = "fixed number of times")
AND status = 'active'
Upvotes: 2