I have generated a dataset that contains data spanning thirty days. Im trying to issolate new data elements that have appeared in the last 2 days but not in the previous 28 days before that.
I run a PHP script that generates the test data. (PHP and MYSQL return the same time when tested)
I run the following query against it.
Results are returned accuretly for aproximetly half an hour. Then despite the fact I believe there to be matching records none are returned when running this query.
Is there any obvious mistake I'm making in the SQL that would cause this apparent 'drift' to occur?
About The Data:
The script generates a 'race' per day. It populates the ranking tables with ranking of the 10 'jokeys'. For the purposes of testing the script generates races from the previous 2 days with 2 new 'jokeys' in the top 10. The remaining 30 days the races are identical.
Results Expected:
The names of two jokeys who have recently ranked in a race (in the last two days and have not ranked in the previous 28).
The SQL:
SELECT *, FROM_UNIXTIME(`race_timestamp`) as ts FROM `rankings`
WHERE `race_venue` = UNHEX(MD5('someplace'))
AND `jokey` IN
(
SELECT `jokey`
FROM `rankings`
WHERE `race_timestamp`
BETWEEN # Get results for races between now and two days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # timestamp two days ago
AND
UNIX_TIMESTAMP() # time stamp now
)
AND
`jokey` NOT IN
(SELECT `jokey`
FROM `rankings`
WHERE `race_timestamp`
BETWEEN # Get results between 2 and 30 days ago
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 30 DAY)) # time stamp 30 days ago
AND
UNIX_TIMESTAMP(DATE_SUB(NOW(), INTERVAL 2 DAY)) # time stamp 2 days ago
)
GROUP BY jockey;
Hope someone can help! Ben