MySQL Help
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsDatabasesMySQL Help

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old December 18th, 2012, 05:08 PM
SeanF SeanF is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Boston, MA USA
Posts: 317 SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 11 m 38 sec
Reputation Power: 43
Send a message via AIM to SeanF Send a message via Yahoo to SeanF
Date calculation in query

Hi

I have a database table full of events and dates and I'm trying to find the last occurrence and age of each event. (Age is obviously the time elapsed since the last occurrence)

So, I have the following query:

Quote:
SELECT max(date) as last,
min(curdate()-date) AS age
FROM event_data
WHERE event = $event
GROUP BY event"

(This Query returns one row of data within a WHILE loop for every event. I think the "GROUP BY" is unnecessary)

A partial list of the results are as follows:

Event - Count - Last - Age
56 - 15 - 2012-10-27 - 191
59 - 13 - 2012-11-17 - 101
44 - 13 - 2012-12-15 - 3
29 - 13 - 2012-11-28 - 90
7 - 12 - 2012-12-08 - 10
5 - 12 - 2012-11-28 - 90

(the "Count" field is the number of occurrences coming from another query)

Today's date is 2012-12-18. For some reason, all of the December ages are correct but the older ones are WAY off.

What have I done wrong?

Reply With Quote
  #2  
Old December 18th, 2012, 05:39 PM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
So this table also stores dates in the future?

Reply With Quote
  #3  
Old December 19th, 2012, 05:26 AM
SeanF SeanF is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: May 2004
Location: Boston, MA USA
Posts: 317 SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level)SeanF User rank is Sergeant Major (2000 - 5000 Reputation Level) 
Time spent in forums: 3 Days 6 h 11 m 38 sec
Reputation Power: 43
Send a message via AIM to SeanF Send a message via Yahoo to SeanF
Quote:
Originally Posted by cafelatte
So this table also stores dates in the future?

Umm... no, I don't think so. The latest date is 2012-12-15, accurately shown as three days ago.

Am I misreading something?

Reply With Quote
  #4  
Old December 19th, 2012, 07:41 AM
cafelatte cafelatte is offline
Contributing User
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Mar 2008
Posts: 1,923 cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level)cafelatte User rank is Major (30000 - 40000 Reputation Level) 
Time spent in forums: 1 Month 5 Days 16 h 21 m 8 sec
Reputation Power: 377
Code:
SELECT event
     , MAX(date) date
     , DATEDIFF(CURDATE(),MAX(date)) age 
  FROM event_data 
 GROUP 
    BY event;


your query subtracts (for instance) the integer 20121101 from 20121208 which (in this case) equals 107.

Reply With Quote
Reply

Viewing: Dev Shed ForumsDatabasesMySQL Help > Date calculation in query

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap