MySQL Where

The WHERE clause is used to filter records, which is to query the data of our specified conditions.

Grammar

    Types ofDetailed explanation
   Basic grammarSelect field from table where where condition;
   ExampleSelect * from money where age = 29;
 Example descriptionQuery all results with age 29 in the MyGuests table
Let us look at the following example:

Instance

The following example will select all rows with FirstName='Mary' from the "MyGuests" table:
<?php
 Header("Content-type:text/html;charset=utf-8"); //Set the encoding
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 $con=mysqli_connect($servername, $username, $password, $dbname);
 / / Check the connection
 If (mysqli_connect_errno())
 {
     Echo "Connection failed: " . mysqli_connect_error();
 }
 
 $result = mysqli_query($con,"SELECT * FROM MyGuests
 WHERE FirstName='Mary'");
 
 While($row = mysqli_fetch_array($result))
 {
     Echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email'];
     Echo "
";
 }
?>

All the data of FirstName='Mary' are queried with the where condition.

We now use phpadmin to add an Age field to the MyGuests table.

Instance

We now use the where clause to query the Age less than 25:

 Header("Content-type:text/html;charset=utf-8"); //Set the encoding
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 $con=mysqli_connect($servername, $username, $password, $dbname);
 / / Check the connection
 If (mysqli_connect_errno())
 {
     Echo "Connection failed: " . mysqli_connect_error();
 }
 
 $result = mysqli_query($con,"SELECT * FROM MyGuests
 WHERE Age<25 ");
 
 While($row = mysqli_fetch_array($result))
 {
     Echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email']."----".$row[' Age'];
     Echo "
";
 }


Can also use the following conditions

Symbol      Description
>more than the
< Less than
 >=greater or equal to
<= Less than or equal to
!= not equal to
= equal

Logical Operators


Symbol       Description
 Oror
And     and
     
Let's look at an example of multiple conditions:

Instance

We will query the data of 'firstname'='Julie' and Age=24.

 Header("Content-type:text/html;charset=utf-8"); //Set the encoding
 $servername = "localhost";
 $username = "root";
 $password = "root";
 $dbname = "test";
 $con=mysqli_connect($servername, $username, $password, $dbname);
 / / Check the connection
 If (mysqli_connect_errno())
 {
     Echo "Connection failed: " . mysqli_connect_error();
 }
 
 $result = mysqli_query($con,"SELECT * FROM MyGuests
 WHERE Firstname='Julie' AND Age=24 ");
 
 While($row = mysqli_fetch_array($result))
 {
     Echo $row['firstname'] . "----" . $row['lastname'] ."----".$row['email']."----".$row[' Age'];
     Echo "
";
 }


Last Updated: 2018-07-06 12:32:44 By akshay

SHARE THIS

share_facebook share_twitter share_google_plus

Please Comment Here :