Mysql and or operators

the MySQL  and  &  or operators are used to filter records based on more than one condition.

the and operator return a result-set record if both the first condition and second condition are true.

the or operator return a result-set record if either first condition or the second condition is true.


database table

below is a selection from the "user" table:

useridfirstnamelastnamemobilenocity
1sandeepkumar9549293245india
2ramkumar9549293212usa
3shyamdhaka2323234545jaipur
4ankitnehra3423567845uae
5radhakumari2323121212jaipur

MySQL And Operator example

the following mysql statement selects all users from the   userid  1   and  the   firstname 'sandeep', in the user table :

 
               select      *          from        user 
      
               where       userid = 1   and   firstname = 'sandeep' ; 
               

output :-

useridfirstnamelastnamemobilenocity
1sandeepkumar9549293245india

MySQL Or Operator example

the following mysql statement selects all users from the   city  "jaipur"  or  the   firstname 'sandeep', in the user table :

 
               select      *          from        user 
      
               where       city = 'jaipur'   or   firstname = 'shyam' ; 
               

output :-

useridfirstnamelastnamemobilenocity
3shyamdhaka2323234545jaipur
5radhakumari2323121212jaipur

combining (  and  &  or )

you can also combine and & or operator using parenthesis.

 
               select      *          from        user 
      
               where       city = 'jaipur'   
               
               and           ( userid <= 3   or   firstname = 'shyam' ); 
               

output :-

useridfirstnamelastnamemobilenocity
3shyamdhaka2323234545jaipur