Solved

Mysql Query to retrieve data from last month range

Posted on 2010-08-19
10
686 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
  • 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
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
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

Ransomware: The New Cyber Threat & How to Stop It

This infographic explains ransomware, type of malware that blocks access to your files or your systems and holds them hostage until a ransom is paid. It also examines the different types of ransomware and explains what you can do to thwart this sinister online threat.  

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Is it safe to dump postgresql database that is in production? 3 38
Database needed for HR files 4 56
java mysql insert application 14 28
Wordpress Query 5 25
Creating and Managing Databases with phpMyAdmin in cPanel.
When it comes to protecting Oracle Database servers and systems, there are a ton of myths out there. Here are the most common.
Video by: Steve
Using examples as well as descriptions, step through each of the common simple join types, explaining differences in syntax, differences in expected outputs and showing how the queries run along with the actual outputs based upon a simple set of dem…
The viewer will learn how to dynamically set the form action using jQuery.

861 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