4
votes

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!

6
are country_name and user_name separate columns? If so try SELECT DISTINCT country_name FROM ...Noah Duncan

6 Answers

9
votes

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.
8
votes

Use DISTINCT:

$sql = "SELECT DISTINCT country_name FROM table";
1
votes

You'll want to use DISTINCT.

SELECT DISTINCT country_name FROM table
1
votes
SELECT DISTINCT(country_name) FROM table;
1
votes
SELECT DISTINCT `country_name` FROM `table_name`
-1
votes

Use

SELECT [ FIELDS ] FROM [ TABLE NAME  ] WHERE ....... GROUP BY country_name