Using two BETWEEN statement

Discussion in 'MySQL' started by mnymkr, May 17, 2007.

  1. #1
    Can you use two BETWEEN statments in a mysql database query....every time i try i get an error.
     
    mnymkr, May 17, 2007 IP
  2. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #2
    Yes you can, but it may not necessarily be the best alternative.
    How does the query look that gives errors?
    Easier to help when you can see it :)
     
    wing, May 17, 2007 IP
  3. mnymkr

    mnymkr Well-Known Member

    Messages:
    2,328
    Likes Received:
    32
    Best Answers:
    0
    Trophy Points:
    120
    #3
    $sql = "SELECT * FROM diamonds
    WHERE carat BETWEEN 1 AND 2
    WHERE girdle BETWEEN 58 AND 60 ";
     
    mnymkr, May 17, 2007 IP
  4. The Stealthy One

    The Stealthy One Well-Known Member Affiliate Manager

    Messages:
    3,043
    Likes Received:
    54
    Best Answers:
    0
    Trophy Points:
    105
    #4
    Not sure on this (depends on whether it's Oracle or MySQL, but here are two things to try, alone and combined).

    Add a semi-colon after the second and third lines, and remove the quotations. Add an AND to the beginning of the third line.

    Let me know if none of those work.
     
    The Stealthy One, May 17, 2007 IP
  5. wing

    wing Active Member

    Messages:
    210
    Likes Received:
    14
    Best Answers:
    0
    Trophy Points:
    58
    #5
    $sql = "SELECT * FROM diamonds
    WHERE carat BETWEEN 1 AND 2
    AND girdle BETWEEN 58 AND 60 ";
     
    wing, May 17, 2007 IP
  6. Mr D

    Mr D Peon

    Messages:
    35
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #6
    for performance you can better not user a * in your query
     
    Mr D, May 18, 2007 IP
  7. TopHosting.us

    TopHosting.us Peon

    Messages:
    66
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #7
    Could also be an 'OR' as well as an 'AND'. Depending on what you are going for, of course.
     
    TopHosting.us, May 28, 2007 IP
  8. zonzon

    zonzon Peon

    Messages:
    100
    Likes Received:
    6
    Best Answers:
    0
    Trophy Points:
    0
    #8
    you can use multiple BETWEEN (from my tests). If used on the same field, il will give you the intersection or the two between

    if used on two different fields it works like a charm!
     
    zonzon, May 31, 2007 IP