how to select with condition, if no condition set, select everything?

Discussion in 'MySQL' started by aayybb, Mar 16, 2012.

  1. #1
    Hi,

    I would like to select only the girls or only 3rd graders in a school in a query. But if there is no criteria being set, (no requirement to select girls or 3rd graders then select every student.
    How would I go about this in a sql?


    Thanks for help in advanced.
     
    aayybb, Mar 16, 2012 IP
  2. Lavi

    Lavi Greenhorn

    Messages:
    5
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    11
    #2
    Hi,Do you still need help on this?
     
    Lavi, Mar 17, 2012 IP
  3. Layoutzzz

    Layoutzzz Greenhorn

    Messages:
    78
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    18
    #3
    Please post your table scheme. It will help create mysql query.
     
    Layoutzzz, Mar 20, 2012 IP
  4. Dwaighty

    Dwaighty Peon

    Messages:
    358
    Likes Received:
    10
    Best Answers:
    0
    Trophy Points:
    0
    #4
    More details are needed indeed, but i think you need something like if (girls condition) cond = " sex = feminin " elseif (3rd grader condition) cond = " grade = 3" else cond = " 1 ".
     
    Dwaighty, Mar 20, 2012 IP
  5. mrandre

    mrandre Peon

    Messages:
    15
    Likes Received:
    0
    Best Answers:
    0
    Trophy Points:
    0
    #5
    if (!$searchstring==''){
    if ($searchstring=='female') {
    $user_cond = "gender='$searchstring'";
    } else {
    $user_cond = "grade='$searchstring";
    }
    } else $user_cond='1';

    $result = mysql_query("SELECT `id`,`grade`,`gender` FROM students WHERE
    $user_cond
    ") or die (mysql_error());
     
    Last edited: Apr 1, 2012
    mrandre, Apr 1, 2012 IP