Author Topic: Using PHP to display MySQL data from two different dates in the same row.  (Read 341 times)

0 Members and 1 Guest are viewing this topic.

Offline bingwalkerTopic starter

  • Irregular
  • Posts: 6
    • View Profile
I need to be able to compare data from today and data from yesterday within the same row, same while loop.

For instance, for yesterday I would have 300 twitter followers and today I had 310 followers, so my +/- column would by +10, or 3% or whatever.  But I can't figure out how to do this in the loop.  The query currently is:

SELECT m.date,m.user,m.followers,m.following,m.tweets,u.avatar FROM metrics m,users u where m.user=u.user and m.date=CURDATE() and u.acct_id=1 order by m.followers desc;

Then I want to use:

SELECT followers FROM metrics where date=DATE_ADD(CURDATE(), INTERVAL -1 DAY);

...to get yesterday's total followers.  And then compare the follower numbers from the two dates.

Can't figure it out.  Any ideas?

Offline fenway

  • MySQL Si-Fu / PHP Resident Alien
  • Global Moderator
  • 'Mind Boggling!'
  • *
  • Posts: 15,443
  • Gender: Male
    • View Profile
I don't see why you need it in "a single row, while loop" -- who cares?  Besides, it's easier to write as two subqueries, though there is a fancy way to get this in a single query. 
:anim_rules: Seriously... if people don't start reading this before posting, I'm going to consider not answering at all.

Offline bingwalkerTopic starter

  • Irregular
  • Posts: 6
    • View Profile
I don't see why you need it in "a single row, while loop" -- who cares?  Besides, it's easier to write as two subqueries, though there is a fancy way to get this in a single query. 

Subqueries.  I hadn't thought of that.  I'm really a noob right now.  I'll research that, any quick ideas on how to write that query with the subquery?