How To Use SQL SELECT DISTINCT Statement Using PHP

admin_img Posted By Bajarangi soft , Posted On 18-09-2020

SQL SELECT DISTINCT Statement used to select similar values only once from that column or DISTINCT statement is used to return only distinct (different) values.

distinct_value

In a table, a column contains many duplicate values; and sometimes you only want to list the different (distinct) values.

Syntax of SQL DISTINCT function and Usage:

SELECT DISTINCT column1, column2, ...
FROM table_name;

Selecting distinct value from the columns in the table.

Let's learn.

1.Create database and table
Table student
StudentID StudentName ParentsContact Address Country DOB Section

  1
    Liam 10101010 23 MG road
Banglore  
INDIA 12/06 A
  2    Andrew 20202020 2415 Anthony  market str Germany 01/02 A
  3    Joshua 30303030 26547 David church road USA 18/04 B
 4     Ryan 40404040 18 Krishna sq.
Banglore
INDIA 26/12 B
 5    Charles 50505050 878 Eli Berguvsvägen 8 INDIA 23/11 C

2.Select without DISTINCT statment.
SELECT Country FROM Student;

3.Select DISTINCT value from column country of student table.
SELECT DISTINCT Country FROM Student;

4.Select DISTINCT value from column country of student table and also COUNT function  it.
SELECT COUNT(DISTINCT Country) FROM Student;//count the number of countries



SELECT Count(*) AS DistinctCountries
FROM (SELECT DISTINCT Country FROM Student);// giving cloumn name for counted countries

5.Create index.php and implement code in PHP script to select DISTINCT values from column.
 
$sql = "SELECT DISTINCT Country FROM Student";
$result = mysql_query($sql);
....

    while ($row = mysql_fetch_array($result)) {
  ...
    }

$sql = "SELECT COUNT(DISTINCT Country) FROM Student";// counts
$result = mysql_query($sql);
.... 
while ($row = mysql_fetch_array($result)) { 
   ... 
}


6.Complete code of SQL SELECT DISTINCT Statement in PHP script.
<!DOCTYPE html>
<html lang="en">
<head>
    <title>How to use  SQL SELECT DISTINCT Statement in PHP</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">
    <script src="https://ajax.googleapis.com/ajax/libs/jquery/3.5.1/jquery.min.js"></script>
    <script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.1/js/bootstrap.min.js"></script>
</head>
<body>
<div class="container">
    <div class="text-center">
        <h1>SQL SELECT DISTINCT Statement in PHP</h1>
    </div>
    <div class="well">
        <?php
        $connect = mysqli_connect("localhost", "root", "", "school_management");

        if ($connect->connect_error) {
            die("Connection failed: " . $connect->connect_error);
        }
        $sql = "SELECT DISTINCT Country FROM Student";


        if($data = mysqli_query($connect, $sql)){
            if(mysqli_num_rows($data ) > 0){
                echo "<table class='table'>
                    <tr>
                    <th>Country</th>
                    </tr>
                    ";
                while($row = $data ->fetch_assoc()){
                    echo "<tr>
                  <td>" . $row['Country'] . "</td>";
                    echo "<td></td>
                  </tr>";
                }
                echo "</table>";
            } else{
                echo "No records found.";
            }
        }
        ?>
    </div>
    <br>
</div>
</body>
</html>

Related Post