Solved

SQL Syntax Question

Posted on 2013-01-21
4
331 Views
Last Modified: 2013-01-21
Hi all,

I am using the following to get the database values in the past 24 hours.

timestampadd(hour, -24, now()

Could someone tell me what to change in this so I can get them for the past week, the past month, and the past year?

Thank you so much,

D
0
Comment
Question by:TLN_CANADA
  • 3
4 Comments
 
LVL 12

Expert Comment

by:Tony303
ID: 38803623
Does this work?

timestampadd(ww, -1, now() for week?
timestampadd(MM, -1, now() for month?
timestampadd(YY, -1, now() for year?
0
 

Author Comment

by:TLN_CANADA
ID: 38803909
I don't think so,

here is the line:

$query12 = mysql_query("SELECT * FROM exercising_table WHERE exercise_timestamp  > timestampadd(ww, -1, now())");
	$total_exercises_today2 = mysql_num_rows($query12);

Open in new window


and the error:

Warning: mysql_num_rows() expects parameter 1 to be resource, boolean given in /home/clear555/public_html/dashboardstats.php on line 51
0
 
LVL 12

Accepted Solution

by:
Tony303 earned 500 total points
ID: 38803923
OK,

2 things.

1.
Perhaps we cannot use WW for week, MM for Month and YY for Year?
Following on from your example that works...

timestampadd(Week, -1, now() for week
timestampadd(Month, -1, now() for month
timestampadd(Year, -1, now() for year

2.
Writing pure SQL would also not have now() for current date time.
It would be GETDATE().

But since your "daily" syntax works using hour, -24 , Now(). I would be loath to change it.

T
0
 
LVL 12

Expert Comment

by:Tony303
ID: 38803950
Sorry, NOW() is the valid date for current date time in My SQL.
Forget #2 in my previous post.


See...http://dev.mysql.com/doc/refman/5.5/en/date-and-time-functions.html

TIMESTAMPADD(unit,interval,datetime_expr)

 Adds the integer expression interval to the date or datetime expression datetime_expr. The unit for interval is given by the unit argument, which should be one of the following values: MICROSECOND (microseconds), SECOND, MINUTE, HOUR, DAY, WEEK, MONTH, QUARTER, or YEAR.

It is possible to use FRAC_SECOND in place of MICROSECOND, but FRAC_SECOND is deprecated. FRAC_SECOND was removed in MySQL 5.5.3.

The unit value may be specified using one of keywords as shown, or with a prefix of SQL_TSI_. For example, DAY and SQL_TSI_DAY both are legal.
mysql> SELECT TIMESTAMPADD(MINUTE,1,'2003-01-02');
        -> '2003-01-02 00:01:00'
mysql> SELECT TIMESTAMPADD(WEEK,1,'2003-01-02');
        -> '2003-01-09'
0

Featured Post

Complete VMware vSphere® ESX(i) & Hyper-V Backup

Capture your entire system, including the host, with patented disk imaging integrated with VMware VADP / Microsoft VSS and RCT. RTOs is as low as 15 seconds with Acronis Active Restore™. You can enjoy unlimited P2V/V2V migrations from any source (even from a different hypervisor)

Join & Write a Comment

Suggested Solutions

Foreword In the years since this article was written, numerous hacking attacks have targeted password-protected web sites.  The storage of client passwords has become a subject of much discussion, some of it useful and some of it misguided.  Of cou…
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to dynamically set the form action using jQuery.

760 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now