PHP MySQL Select Distinct Statement Example

PHP MySQL Select Distinct Statement Example


Posted in : PHP Posted on : February 4, 2011 at 5:33 PM Comments : [ 0 ]

If you want to fetch data(records) without duplicate value from the database table then use "Select Distinct" statement.

PHP MySQL Select Distinct Statement Example:

If you want to fetch data(records) without duplicate value from the database table then use "Select Distinct" statement. The "Select Distinct"  is used to fetch the different value in the record set. Suppose the database table contains some duplicate values but you want to fetch records without duplicate values then used "Select Distinct".

Syntax:

SELECT DISTINCT column_name FROM table_name

Example:

<?php
$connection = mysql_connect("localhost","root","root");

if (!$connection) {
die('PHP Mysql database connection could not connect : ' . mysql_error());
}
else{ 
$db_name = "php_mysql";

mysql_select_db($db_name, $connection);
$sql = "select DISTINCT user_name, user_email from user;";
$records = mysql_query($sql, $connection); 
while($rows = mysql_fetch_array($records)){
echo "User Name : " . $rows['user_name'] . "<br />"; 
echo "User Email : " . $rows['user_email'] . "<br /><br />"; 
}
}
mysql_close($connection); 
?> 

The database user table is:

In this example we have used user_name and user_email with the "Distinct" statement so the result will came without repeat user name and email.

Output:

User Name : Brijesh
User Email : brijesh@gmail.com

User Name : Brijesh
User Email : brijeshkumar@gmail.com

User Name : Avanish
User Email : avanish@gmail.com

User Name : Ravi
User Email : ravi@gmai.com

Download Example Code

Go to Topic «PreviousHomeNext»

Your Comment:


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 

 
Tutorial Topics