Limit rows based on a value?

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • BaseballGraphs
    New Member
    • Sep 2010
    • 75

    Limit rows based on a value?

    Hello,

    I am trying to filter an array that I am pulling from a mySQL database using PHP.

    My question is if there is a way to LIMIT the number of rows that are pulled from the database with a mathematical operation in the SQL statement. I think it would something like:

    Code:
    $sql = sprintf(SELECT * FROM data WHERE day = CURdate()-INTERVAL 1 day AND statistic_id = %d ORDER BY value DESC LIMIT value > %d, $statistic_id, $selected_value);
    My objective is to be able to filter the array after a user selects a value from a dropdown option list. The value in the drop down would be stored in the URL and $selected_value would be determined by this value in the URL by the _GET method.

    Any help on this would be appreciated!
    Thanks!
  • Oralloy
    Recognized Expert Contributor
    • Jun 2010
    • 988

    #2
    Yes, but you need to include the mathematical relationship as part of your where clause, like so:

    Code:
    $sql = sprintf(SELECT * 
                     FROM data
                     WHERE (day = CURdate()-INTERVAL 1 day)
                       AND (statistic_id = %d)
                       AND (value > %d)
                     ORDER BY value DESC,
                   $statistic_id,
                   $selected_value);
    p.s. If I screwed up the PHP code, forgive me, I'm trying to convey the structure of the SQL, and I don't really know PHP that well.

    Comment

    Working...