Mysql Query without Repeat!
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开发者_JAVA技巧 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!
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.
Use DISTINCT:
$sql = "SELECT DISTINCT country_name FROM table";
You'll want to use DISTINCT
.
SELECT DISTINCT country_name FROM table
SELECT DISTINCT(country_name) FROM table;
SELECT DISTINCT `country_name` FROM `table_name`
Use
SELECT [ FIELDS ] FROM [ TABLE NAME ] WHERE ....... GROUP BY country_name
精彩评论