?
Solved

Mysql Query to retrieve data from last month range

Posted on 2010-08-19
10
Medium Priority
?
708 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
WordPress Tutorial 2: Terminology

An important part of learning any new piece of software is understanding the terminology it uses. Thankfully WordPress uses fairly simple names for everything that make it easy to start using the software.

 
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 2000 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

Free Tool: Port Scanner

Check which ports are open to the outside world. Helps make sure that your firewall rules are working as intended.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

In this blog post, we’ll look at how using thread_statistics can cause high memory usage.
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…
The viewer will learn how to count occurrences of each item in an array.

765 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