Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Mysql Query without Repeat!

Tags:

php

mysql

Hi I will Explain what i want...

I have a table with records like this

...........................

country_name - username

India - abc1

Australia - abc2

India - abc3

USA - abc4

Australis - abc5

Lebanon - abc6

...........................

From Above Table I need to get country list without repeat, is there any chance to get like this...

Ex Code:

$sql = 'bla bla bla bla bla';

$res = mysql_query($sql);

while($row = mysql_fetch_array($res)){
    echo $row['country_name'].'<br /><br />';
}

Ex Output(Like this):

India

Australia

USA

Lebanon

If is there any change to solve my issue please tell me and advance thanks for that!

like image 701
rkaartikeyan Avatar asked Dec 02 '22 02:12

rkaartikeyan


2 Answers

Change your code to this -

$sql = "SELECT distinct country_name from my_table";
$res = mysql_query($sql);

while($row = mysql_fetch_array($res))
{
    echo $row['country_name'].'<br /><br />';
}

From this link -

DISTINCT helps to eliminate duplicates. If a query returns a result that contains duplicate rows, you can remove duplicates to produce a result set in which every row is unique. To do this, include the keyword DISTINCT after SELECT and before the output column list.
like image 65
MD Sayem Ahmed Avatar answered Dec 05 '22 12:12

MD Sayem Ahmed


Use DISTINCT:

$sql = "SELECT DISTINCT country_name FROM table";
like image 43
sagi Avatar answered Dec 05 '22 14:12

sagi