I have built a simple website that grabs data from MySQL. The site uses the Twitter API to grab data about a users account, i.e. number of followers. I am fetching the data once a day and storing the results as a new entry in the table. The goal is to be able to show trending over time. I have successfully create a MySQL query to display the latest result but now would like to compare the latest entry with the last entry (or an entry from a specific date) to see how it has changed.
I would probably do it in mysql, because I like having as much number crunching as possible done on the db side, and php pretty much outputting it, though it often makes the query more complex. If you’re using really large amounts of data, mysql will probably do the calculations faster than bringing in a lot of data an looping through. Of course you can also do it largely in php. In this case there’s not really a should.
An example query - compare followers since the last check
SELECT tw1.Twitter_handle, MAX(tw1.last_update) AS last_update,
(
SELECT tw2.followers
FROM twitter_followers tw2
WHERE
tw2.Twitter_handle=tw1.twitter_handle AND
tw2.last_update = MAX(tw1.last_update)
) AS last_followers,
(
SELECT MAX(tw3.last_update)
FROM twitter_followers tw3
WHERE
tw3.Twitter_handle=tw1.twitter_handle AND
tw3.last_update<MAX(tw1.last_update)
) AS prev_update,
(
SELECT tw4.followers
FROM twitter_followers tw4
WHERE
tw4.Twitter_handle=tw1.twitter_handle AND
tw4.last_update=(
SELECT MAX(tw3.last_update)
FROM twitter_followers tw3
WHERE
tw3.Twitter_handle=tw1.twitter_handle
AND tw3.last_update<MAX(tw1.last_update)
)
) AS prev_followers
FROM twitter_followers tw1 GROUP BY tw1.Twitter_handle
you should use the method that performs better, and is easiest to manipulate with regards to the original specs, namely “or an entry from a specific date”
i’m pretty good with sql, but i sure wouldn’t want to try to modify your query to meet those specs