jakebur01 Posted March 4, 2009 Share Posted March 4, 2009 How can I select the unique values in a column? For instance: If I have United States on 4 rows, Peru on 3, and Argentina on 5.......... but only want to select them once and order them.... like Argentina, Peru, United States. I am trying to list users by country. Something like: select countries from db while{ echo"<h3>$row[country]</h3><br />"; select * users where $row[country] while{ echo"$row[company]<br />$row[address]<br />......... etc } { Quote Link to comment https://forums.phpfreaks.com/topic/147976-solved-list-users-based-on-country/ Share on other sites More sharing options...
Maq Posted March 4, 2009 Share Posted March 4, 2009 SELECT DISTINCT country FROM table; Then have a second query inside the while loop to grab every user in that country like you have in your example. Quote Link to comment https://forums.phpfreaks.com/topic/147976-solved-list-users-based-on-country/#findComment-776640 Share on other sites More sharing options...
samshel Posted March 4, 2009 Share Posted March 4, 2009 <?php $sql = "SELECT * FROM table GROUP BY country, order by country"; $res = mysql_query($sql); $strCurrentCountry =""; while ($row = mysql_fetch_array($res)){ if($row['country'] != $strCurrentCountry) { $strCurrentCountry = $row['country']; echo"<h3>$row[country]</h3><br />"; } echo " $row[company]<br />$row[address]<br /> } ?> something like this... Quote Link to comment https://forums.phpfreaks.com/topic/147976-solved-list-users-based-on-country/#findComment-776641 Share on other sites More sharing options...
jakebur01 Posted March 4, 2009 Author Share Posted March 4, 2009 I used the DISTINCT query. Thank you. Quote Link to comment https://forums.phpfreaks.com/topic/147976-solved-list-users-based-on-country/#findComment-776660 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.