I'm trying to write a query to find an eligble key. The criteria for an key being eligble is that it must not have been used 10 times within 24 hrs.
Every time a key has been used, a record is saved in my table api_history.
Can anyone please teach me how to do it correctly? Currently I'm getting an empty key returned, as no records exists in the api_history. (Then it should just have returned the first giving key).
Thanks in advance!
Query:
SELECT ak.key
FROM api_history ah
INNER JOIN api_keys ak ON ah.key_id = ah.id
WHERE ah.used_at > DATE_SUB(now(), INTERVAL 1 DAY)
HAVING COUNT(ah.id) < 10 LIMIT 0,1
Tables:
api_keys
- id (int)
- key (string)
api_history
- id (int)
- key_id (int)
- used_at (datetime)