Solved

Mysql Query to retrieve data from last month range

Posted on 2010-08-19
10
692 Views
Last Modified: 2013-12-13
HI,

I have the attached MySql Query, "post.dateline" is the date field. I want to add a WHERE clause to get only the records 40 days old or newer.

How to say?

Thanks
SELECT thread.dateline AS dateline_1, thread.threadid, thread.title, thread.lastpostid, thread.forumid, post.username, post.userid, post.dateline, thread.views, post.pagetext, thread.postusername, thread.replycount
FROM (thread LEFT JOIN post ON post.postid=thread.lastpostid)

Open in new window

0
Comment
Question by:Fernanditos
[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
  • 4
  • 3
  • 2
  • +1
10 Comments
 
LVL 14

Expert Comment

by:Kalpan
ID: 33472838
SELECT thread.dateline AS dateline_1, thread.threadid, thread.title, thread.lastpostid, thread.forumid, post.username, post.userid, post.dateline, thread.views, post.pagetext, thread.postusername, thread.replycount
FROM (thread LEFT JOIN post ON post.postid=thread.lastpostid)
WHERE
post.dateline < NOW() - INTERVAL 40 DAY


0
 
LVL 8

Expert Comment

by:kingjely
ID: 33472858

< I want to add a WHERE clause to get only the records 40 days old or newer.



SELECT thread.dateline AS dateline_1, thread.threadid, thread.title, thread.lastpostid, thread.forumid, post.username, post.userid, post.dateline, thread.views, post.pagetext, thread.postusername, thread.replycount
FROM (thread LEFT JOIN post ON post.postid=thread.lastpostid)
WHERE post.dateline between (NOW() - INTERVAL 40 DAY) and (NOW() + INTERVAL 40 DAY)
0
 

Author Comment

by:Fernanditos
ID: 33473041
Both solutions are not working:

I forgot to say that "dateline" date-time format looks like:

1268262191 on database (that would be March 27, 2010, 11:37 am)

Any idea?
0
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 14

Expert Comment

by:Kalpan
ID: 33473129
ELECT thread.dateline AS dateline_1, thread.threadid, thread.title, thread.lastpostid, thread.forumid, post.username, post.userid, post.dateline, thread.views, post.pagetext, thread.postusername, thread.replycount
FROM (thread LEFT JOIN post ON post.postid=thread.lastpostid)
WHERE
DATE_FORMAT(post.dateline,'%d/%m/%Y') < NOW() - INTERVAL 40 DAY
0
 
LVL 8

Expert Comment

by:kingjely
ID: 33473137


what timestamp is that?

0
 

Author Comment

by:Fernanditos
ID: 33473372
"DATE_FORMAT(post.dateline,'%d/%m/%Y') < NOW() - INTERVAL 40 DAY" returns non rows, even if there are rows from 1 day old.

The "dateline" filed is a vbulletin standard date/time format. in the database is an int(10)
0
 
LVL 8

Expert Comment

by:kingjely
ID: 33473723

Hrrmm..

what about

SELECT thread.dateline AS dateline_1, thread.threadid, thread.title, thread.lastpostid, thread.forumid, post.username, post.userid, post.dateline, thread.views, post.pagetext, thread.postusername, thread.replycount
FROM (thread LEFT JOIN post ON post.postid=thread.lastpostid)
WHERE
DATE_FORMAT(post.dateline,'%d/%m/%Y') between NOW() and NOW() - INTERVAL 40 DAY
0
 

Author Comment

by:Fernanditos
ID: 33474324
still does not works.
0
 
LVL 143

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 500 total points
ID: 33474652
so, you want something like this:
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_unix-timestamp
http://dev.mysql.com/doc/refman/5.1/en/date-and-time-functions.html#function_date-add


SELECT thread.dateline AS dateline_1, thread.threadid, thread.title, thread.lastpostid, thread.forumid, post.username, post.userid, post.dateline, thread.views, post.pagetext, thread.postusername, thread.replycount
FROM (thread LEFT JOIN post ON post.postid=thread.lastpostid)
WHERE post.dateline >= UNIX_TIMESTAMP( DATE_SUB(NOW() , INTERVAL 40 DAY) )
  AND post.dateline <= UNIX_TIMESTAMP( DATE_ADD(NOW() , INTERVAL 40 DAY) )

Open in new window

0
 
LVL 8

Expert Comment

by:kingjely
ID: 33481522
When i checked the UNIX_TIMESTAMP for '2010-03-27 11:37'
(like your comment said, 1268262191 on database (that would be March 27, 2010, 11:37 am)

UNIX_TIMESTAMP('2010-03-27 11:37')  returns 1269661020 NOT 1268262191

I wonder why? I don't understand
0

Featured Post

U.S. Department of Agriculture and Acronis Access

With the new era of mobile computing, smartphones and tablets, wireless communications and cloud services, the USDA sought to take advantage of a mobilized workforce and the blurring lines between personal and corporate computing resources.

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…
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…
The viewer will learn how to dynamically set the form action using jQuery.
Polish reports in Access so they look terrific. Take yourself to another level. Equations, Back Color, Alternate Back Color. Write easy VBA Code. Tighten space to use less pages. Launch report from a menu, considering criteria only when it is filled…

740 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