SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    mysql php search between min & max value

    Hi,

    I was wondering if someone can help me on how to search a database for all the records that are between 2 values, min and max

    for example, min = 3, max = 10
    i want to be able to get 3,4,5,6,7,8,9,10.

    Thank you
    sang

  2. #2
    SitePoint Member panda's Avatar
    Join Date
    Mar 2003
    Location
    Melbourne, Australia
    Posts
    19
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    MySQL Statement for selection by range (ie. with min, max matching values)

    The MySQL statement you want is:

    SELECT * FROM tablename WHERE field BETWEEN min AND max

    eg. if you had a table called "numbers" with a field "id" (int) and a field "name" (varchar(12)):

    id name
    4 four
    2 two
    10 ten
    16 sixteen

    then the query "SELECT * FROM numbers WHERE id BETWEEN 4 AND 10" would return:

    id name
    4 four
    10 ten

    Relavent section of the MySQL manual:
    http://www.mysql.com/doc/en/Comparison_Operators.html

    Hope that helps :-)

  3. #3
    SitePoint Zealot
    Join Date
    Mar 2002
    Location
    Perth, Australia
    Posts
    164
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by itssangy
    Hi,

    I was wondering if someone can help me on how to search a database for all the records that are between 2 values, min and max

    for example, min = 3, max = 10
    i want to be able to get 3,4,5,6,7,8,9,10.

    Thank you
    sang
    You could also use:
    Code:
    SELECT * FROM table WHERE field >= 3 AND field <= 10
    I would still go with panda's suggestion though

  4. #4
    SitePoint Enthusiast
    Join Date
    Jun 2001
    Location
    Tacoma, WA
    Posts
    34
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Thanks!

    both ways work.

    Sang


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
  •