MySQL AND & OR

The AND operator displays a record if both the first condition and the second condition is true.

The OR operator displays a record if either the first condition or the second condition is true.

Example for AND :

Now we want to select only the record with the name equal to “learnwebdevelopment” AND the id equal to 1:

We use the following SELECT statement:

<?php
$select_q = "SELECT * FROM employee WHERE name='learnwebdevelopment' AND id=1";
$select_r = mysql_query($select_q);
?>

Example for OR:

Now we want to select only the record with the name equal to “learnwebdevelopment” OR the name equal to “Qpt”:

We use the following SELECT statement:

<?php
$select_q = "SELECT * FROM employee WHERE name='learnwebdevelopment' OR name='Qpt'";
$select_r = mysql_query($select_q);
?>

Combining AND & OR :

You can also combine AND and OR (use parenthesis to form complex expressions). Now we want to select only the records with the name equal to “learnwebdevelopment” AND the age equal to 25 OR to 26. We use the following SELECT statement:

<?php
$select_q = "SELECT * FROM employee WHERE name='learnwebdevelopment' AND (age=25 OR age=26)";
$select_r = mysql_query($select_q);
?>

Posted in MySQL