Select Records From MySql on PHP
I have a table with records like this
country_name username
------------------------
India abc1
Austra开发者_如何转开发lia 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
Try this:
SELECT DISTINCT country_name FROM table
The group by method is better if you want to select more than just the country name with your query...like: $sql = "SELECT country_name, username FROM table GROUP BY country_name;
but if you just want to select country name, DISTINCT is slightly faster.
$sql = "SELECT DISTINCT country_name FROM table";
精彩评论