Expiring Today—Celebrate National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

SQL Syntax Question

Posted on 2013-01-21
4
Medium Priority
?
344 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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 2000 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

Get your Disaster Recovery as a Service basics

Disaster Recovery as a Service is one go-to solution that revolutionizes DR planning. Implementing DRaaS could be an efficient process, easily accessible to non-DR experts. Learn about monitoring, testing, executing failovers and failbacks to ensure a "healthy" DR environment.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
In this article, we’ll look at how to deploy ProxySQL.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
In this video, Percona Solutions Engineer Barrett Chambers discusses some of the basic syntax differences between MySQL and MongoDB. To learn more check out our webinar on MongoDB administration for MySQL DBA: https://www.percona.com/resources/we…

719 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