Jump to content

Selecting different time periods from SQL Query


defroster

Recommended Posts

Hello,

 

I have the following query:

$sql = "Select photos.*, categories.cat FROM photos, categories
WHERE photos.cat_id = categories.id AND photos.status ='1'	ORDER BY (videos.up-videos.down) DESC
LIMIT $start, $limit";
$result = mysql_query($sql);

 

The photos table contains one column called "dateposted' which is in the following format (2010-08-03 00:15:00).

 

How do I from this query select different timeframes by using "hours" something like this I am guessing??:

 

Day
WHERE dateposted=time(now)-24(hours)
Week
WHERE dateposted=time(now)-168(hours)
Month
WHERE dateposted=time(now)-744(hours)
Year
WHERE dateposted=time(now)-8760(hours)

 

Thanks so much. /df

Link to comment
Share on other sites

Thanks so much, but now I am stuck again. How do I do two 'AND' in a SQL Query.. Sorry I am not too good at this. Still learning.

 

$sql = "Select photos.*, categories.cat FROM photos, categories
WHERE photos.cat_id = categories.id AND photos.status ='1' AND DATE_SUB(videos.dateposted, INTERVAL 24 HOUR) ORDER BY (videos.up-videos.down) DESC
LIMIT $start, $limit";
$result = mysql_query($sql);

 

I have tried this but it still selects all videos instead of the past 24 hours.. Thanks!

Link to comment
Share on other sites

$sql = "Select photos.*, categories.cat FROM photos, categories
WHERE photos.cat_id = categories.id AND photos.status ='1' AND videos.dateposted >= DATE_SUB(videos.dateposted, INTERVAL 24 HOUR) ORDER BY (videos.up-videos.down) DESC
LIMIT $start, $limit";
$result = mysql_query($sql);

 

Anyway, i cant see "videos" in your query. And date sub only return a date -> date = videos.dateposted - 24 hour

Link to comment
Share on other sites

Thanks so much. But now instead the return is NULL even though I have a post that has a  dateposted within 24 hours :( Any ideas what could be wrong?

 

This is exactly what the query looks like now.

$sql = "Select videos.*, categories.cat FROM videos, categories
WHERE videos.cat_id = categories.id AND videos.status ='1' AND videos.dateposted >= DATE_SUB(videos.dateposted, INTERVAL 24 HOUR) ORDER BY (videos.up-videos.down) DESC
LIMIT $start, $limit";
$result = mysql_query($sql);

Link to comment
Share on other sites

Sorry, wrong the return is not NULL, it displays all videos instead. But not only the ones the past 24 hours which I want to display?? Hmm.. Thanks so much for help. Any ideas what I am doing wrong?

 

Current code:

$sql = "Select videos.*, categories.cat FROM videos, categories
WHERE videos.cat_id = categories.id AND videos.status ='1' AND videos.dateposted >= DATE_SUB(videos.dateposted, INTERVAL 24 HOUR) ORDER BY (videos.up-videos.down) DESC
LIMIT $start, $limit";
$result = mysql_query($sql);

Link to comment
Share on other sites

You're comparing a field value to itself?!?!

 

Yeah, its my fault :) Of course it should be "NOW()"

 

Select videos.*, categories.cat FROM videos, categories
WHERE videos.cat_id = categories.id AND videos.status ='1' AND videos.dateposted >= DATE_SUB(NOW(), INTERVAL 24 HOUR) ORDER BY (videos.up-videos.down) DESC
LIMIT $start, $limit;

Link to comment
Share on other sites

  • 3 weeks later...
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.