Solved

SQL Syntax Question

Posted on 2013-01-21
4
340 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 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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

When table data gets too large to manage or queries take too long to execute the solution is often to buy bigger hardware or assign more CPUs and memory resources to the machine to solve the problem. However, the best, cheapest and most effective so…
Containers like Docker and Rocket are getting more popular every day. In my conversations with customers, they consistently ask what containers are and how they can use them in their environment. If you’re as curious as most people, read on. . .
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…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

615 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