SELECT column1, column2, ...
FROM table_name
WHERE condition1 AND condition2 AND condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE condition1 OR condition2 OR condition3 ...;
SELECT column1, column2, ...
FROM table_name
WHERE NOT condition;
StudentID | StudentName | ParentsContact | Address | Country | DOB | Section |
Branch |
1 |
Liam | 10101010 | 23 MG road Banglore |
INDIA | 12/06 | A | CSE |
2 | Andrew | 20202020 | 2415 Anthony market str | Germany | 01/02 | A | ISE |
3 | Joshua | 30303030 | 26547 David church road | USA | 18/04 | B | CL |
4 | Ryan | 40404040 | 18 Krishna sq. Banglore |
INDIA | 26/12 | B | EE |
5 | Charles | 50505050 | 878 Eli Berguvsvägen 8 | INDIA | 23/11 | C | ECE |
$sql_1 = "SELECT * FROM Student WHERE Country='INDIA' AND Section='A'";
if($result_1 = mysqli_query($connect, $sql_1)){
if(mysqli_num_rows($result_1) > 0){
...
}
}
$sql_2= "SELECT * FROM Student WHERE Section='A' OR Section='c'";
if($result_2 = mysqli_query($connect, $sql_2)){
if(mysqli_num_rows($result_2) > 0){
...
}
}
$sql_3 = "SELECT * FROM Student WHERE NOT Country='INDIA'";
if($result_3 = mysqli_query($connect, $sql_3)){
if(mysqli_num_rows($result_3) > 0){
...
}
}
<!DOCTYPE html>
<html lang="en">
<head>
<title>How to use SQL AND & OR,NOT Operators</title>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/css/bootstrap.min.css">
</head>
<body>
<div class="container">
<br>
<br>
<br>
<div class="text-center">
<h1>SQL AND & OR,NOT Operators</h1>
</div>
<div class="well">
<?php
$connect = mysqli_connect("localhost", "root", "", "school_management");
if ($connect->connect_error) {
die("Connection failed: " . $connect->connect_error);
}
$sql_1 = "SELECT * FROM Student
WHERE Country='INDIA' AND Section='A'";
$sql_2= "SELECT * FROM Student WHERE Section='A' OR Section='c'";
$sql_3 = "SELECT * FROM Student WHERE NOT Country='INDIA'";
if($result_1 = mysqli_query($connect, $sql_1)){
if(mysqli_num_rows($result_1) > 0){
echo "<table class='table'>
<tr>
<th>StudentID</th>
<th>StudentName</th>
<th>ParentsContact</th>
<th>Address</th>
<th>Country</th>
<th>DOB</th>
<th>Section</th>
<th>Branch</th>
</tr>
";
while($row = $result_1->fetch_assoc()){
echo "<tr>
<td>" . $row['StudentID'] . "</td>
<td>".$row['StudentName'] ."</td>
<td>".$row['ParentsContact'] ."</td>
<td>".$row['Address'] ."</td>
<td>".$row['Country'] ."</td>
<td>".$row['DOB'] ."</td>
<td>".$row['Section'] ."</td>
<td>".$row['Branch'] ."</td>
</tr>";
}
echo "</table>";
} else{
echo "No records found.";
}
}
echo"<br>";
if($result_2 = mysqli_query($connect, $sql_2)){
if(mysqli_num_rows($result_2) > 0){
echo "<table class='table'>
<tr>
<th>StudentID</th>
<th>StudentName</th>
<th>ParentsContact</th>
<th>Address</th>
<th>Country</th>
<th>DOB</th>
<th>Section</th>
<th>Branch</th>
</tr>
";
while($row = $result_2->fetch_assoc()){
echo "<tr>
<td>" . $row['StudentID'] . "</td>
<td>".$row['StudentName'] ."</td>
<td>".$row['ParentsContact'] ."</td>
<td>".$row['Address'] ."</td>
<td>".$row['Country'] ."</td>
<td>".$row['DOB'] ."</td>
<td>".$row['Section'] ."</td>
<td>".$row['Branch'] ."</td>
</tr>";
}
echo "</table>";
} else{
echo "No records found.";
}
}
echo"<br>";
if($result_3 = mysqli_query($connect, $sql_3)){
if(mysqli_num_rows($result_3) > 0){
echo "<table class='table'>
<tr>
<th>StudentID</th>
<th>StudentName</th>
<th>ParentsContact</th>
<th>Address</th>
<th>Country</th>
<th>DOB</th>
<th>Section</th>
<th>Branch</th>
</tr>
";
while($row = $result_3->fetch_assoc()){
echo "<tr>
<td>" . $row['StudentID'] . "</td>
<td>".$row['StudentName'] ."</td>
<td>".$row['ParentsContact'] ."</td>
<td>".$row['Address'] ."</td>
<td>".$row['Country'] ."</td>
<td>".$row['DOB'] ."</td>
<td>".$row['Section'] ."</td>
<td>".$row['Branch'] ."</td>
</tr>";
}
echo "</table>";
} else{
echo "No records found.";
}
}
?>
</div>
<br>
</div>
</body>
</html>