Results 1 to 6 of 6

Thread: Php mysql Order by Rand() last 30 days

  1. #1
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default Php mysql Order by Rand() last 30 days

    This is the code i have now

    $ransql = bb_db_query("select * from album where active='Y' and find_in_set('117',categories) ORDER BY album_id DESC LIMIT 5");

    I was wondering if its possible to do a ORDER BY RAND() limiting it to the last 30 days.

  2. #2
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    Change your current ORDER BY to RAND(), and add to the WHERE conditions that the time must be greater than 30 days ago. If this is a unix time stamp, then that will be something like: WHERE `time`>($currenttime-(30*24*60*60)), but if your time is stored in another format you'll need to convert it.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  3. #3
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I want to keep the random too how would I do that?? Should I be doing Less then if I want it to be no further then 30 days ago??

  4. #4
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    The WHERE section limits the results to certain entries. The ORDER BY section decides which, of those, is first and next, through the last one. I don't understand the problem. You can use both.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  5. #5
    Join Date
    Jul 2011
    Posts
    36
    Thanks
    1
    Thanked 0 Times in 0 Posts

    Default

    I put in your line of code and got an error

    1064 - You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'WHERE `time`>(-(30*24*60*60)) DESC LIMIT 5' at line 1

    select * from album where active='Y' and find_in_set('117',categories) ORDER BY album_id WHERE `time`>(-(30*24*60*60)) DESC LIMIT 5


    Can you tell me what i did wrong? thanks in advance.

  6. #6
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    There are two problems:

    1. The order of statements in MySQL is important:
    select * from album where active='Y' and find_in_set('117',categories) and `time`>(-(30*24*60*60)) ORDER BY RAND();

    2. What I gave you was an example of the time. You will need to apply this for your system. How do you store the time? Do you store the time? If not, you will need to add a column for time and then use that to compare. And you will also need to get the real time from PHP and add that. For example:
    "...... `time`>(".time()."-(30*24*60*60)) .......";


    I recommend an introductory MySQL tutorial to learn the basics. Or look at some examples of working code to understand how the queries work. There are also sites that have full documentation for MySQL syntax, but that can be confusing if it's new. Overall, I think MySQL is counterintuitive and confusing when you first start, but you can't do it without understanding it, so be patient and take it one step at a time. MySQL is not "difficult" really... it's just different.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •