Jump to content

Recommended Posts

Can somebody help me with this, I'm not a programmer and am simply looking to get items pulled that are random, but I want them only from the last so many days.

 

I currently have this:

 

$res = ppmysql_query("SELECT * FROM {$Globals['pp_db_prefix']}photos WHERE approved=1 AND cat IN (501,502,511,503,562,504,506,505,508,2,510,509) ORDER BY rand() LIMIT 30", $link);

 

which pulls from my proper categories, and gives me random results, but they are random and some of the results are from years ago. I want the same thing, but I want results that are within the last 10 or 30 so days.

 

Can somebody help me with this?

 

After searching, it looks like people don't recommend the rand() as "it doesn't scale well", but it's what I have at the moment, and I'm stuck with it until I can afford a programmer to go through my entire code.

 

It just need it so that the way it's currently being done, is left intact, even tho there maybe better ways of doing it, but I need results to only be within a few days timeframe.

 

Thanks for any help you provide me, I'm thinking it's something super simple added to that line.

Link to comment
https://forums.phpfreaks.com/topic/266747-random-from-the-last-30-days/
Share on other sites

This method, with the select subquery, ran in a third of the time on my test

 

SELECT * FROM
(
    SELECT * FROM {$Globals['pp_db_prefix']}photos 
    WHERE approved=1 
        AND cat IN (501,502,511,503,562,504,506,505,508,2,510,509) 
        AND date BETWEEN (CURDATE() - INTERVAL 30 DAY) AND (CURDATE() - INTERVAL 10 DAY)
    ) as x
ORDER BY rand() 
LIMIT 30;

 

        AND date BETWEEN (CURDATE() - INTERVAL 30 DAY) AND (CURDATE() - INTERVAL 10 DAY)

 

I've seen you post that methodology several times before, and I'm just curious: Is there a timestamp format that must be abided by in order for that to work? a la yyyy-mm-dd hh:mm:ss ?

As with any mysql datetime calculation you have to use columns of type DATE, DATETIME or TIMESTAMP.

 

I use that method because I find it more readable and intuitive and easier than remembering the order of the args for DATE_ADD() and DATE_SUB().

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.