Results 1 to 3 of 3

Thread: PHP MySQL - Top 50 Rows Between...

  1. #1
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default PHP MySQL - Top 50 Rows Between...

    Hi There,

    I currently have a PHP/MySQL query script which displays the top 50 MySQL rows in the table, however, it displays the top 50 of all time. I would love a way for it to display the top 50 rows in the last 7 days. The table looks a bit like this,

    Code:
    -------------------------------------
    |id       | tag            |date    |
    -------------------------------------
    |1        | search 1       |05-27-2010
    |2        | search 2       |05-27-2010
    |3        | search 3       |05-28-2010
    |4        | search 4       |05-29-2010
    -------------------------------------
    My current PHP/MySQL script looks like this (the script that grabs the top 50 of all time):

    PHP Code:
    $result mysql_query("SELECT tag, COUNT(*) FROM tags
    GROUP BY tag
    ORDER BY 2 DESC LIMIT 50"
    ); 

  2. #2
    Join Date
    Mar 2010
    Posts
    14
    Thanks
    4
    Thanked 0 Times in 0 Posts

    Default

    Any help guys?

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

    Default

    You are storing the date in a "human readable" format. That's not good for data organization: use a timestamp. The easiest way is to get it using time(). Then you can format that later (when displaying, NOT in the database) using date().

    Once you do that, then all you need to do is add "WHERE time>$startofweektime AND time<$endofweektime"

    Timestamps are hard to get used to, but they're much easier to work with for mathematical operations.


    Alternatively you can keep the current format (I strongly recommend that you change it!), and search for "WHERE time = $day1 OR time = $day2 OR ...... time = $day7".


    Aside from adding the WHERE component, your query remains the same.
    The order is:
    OPERATION [select] / info [tables etc] / WHERE / [order by] / LIMIT

    So:
    "SELECT tag, COUNT(*) FROM tags WHERE ........ GROUP BY tag ORDER BY 2 DESC LIMIT 50"
    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
  •