Jump to content


This topic is now archived and is closed to further replies.


Sorting Retrieved Data

Recommended Posts


I am trying to make the following 3 scripts of code produce output that is in alphabetical order. I understand it needs to contain the statement ORDER BY, however not sure where to place it.

Can someone please assist?
Much appreciated!


$result = mysql_query("[b]SELECT[/b] city_id, City [b]FROM[/b] city [b]WHERE[/b] country_id =".$country_id);


$result = mysql_query("[b]SELECT DISTINCT[/b] category.category_id, Category [b]FROM[/b] category [b]INNER JOIN[/b] cat_city_bus [b]ON[/b] category.category_id = cat_city_bus.category_id [b]WHERE[/b] City_id = ".$city_id,$connection);


$query = "[b]SELECT[/b]directory.business_id as dID, category.Category, Business, Address1, Address2, Address3, city.City, city.State, Postcode, country.Country, Telephone, Website, Email, Details, Picture [b]FROM[/b] cat_city_bus [b]INNER JOIN[/b] category [b]ON[/b] cat_city_bus.category_id = category.category_id
[b]INNER JOIN[/b] city [b]ON[/b] cat_city_bus.city_id = city.city_id
[b]INNER JOIN[/b] directory [b]ON[/b] cat_city_bus.business_id = directory.business_id
[b]INNER JOIN[/b] country [b]ON[/b] city.country_id = country.country_id [b]WHERE[/b]
cat_city_bus.city_id = ".$city_id." [b]AND[/b]
cat_city_bus.category_id = ".$category_id;

$result = mysql_query($query, $connection);


Share this post

Link to post
Share on other sites
The [b]ORDER BY[/b] statement is used at the end of your query like so:
[!--sql--][div class=\'sqltop\']SQL[/div][div class=\'sqlmain\'][!--sql1--][span style=\'color:blue;font-weight:bold\']SELECT[/span] city_id, City [color=green]FROM[/color] [color=orange]city[/color] [color=green]WHERE[/color] country_id [color=orange]=[/color][color=red]'$country_id'[/color] [color=green]ORDER BY[/color] city [color=green]ASC[/color] [!--sql2--][/div][!--sql3--]
change [b]city[/b] to the row name that you want your sql query to order the results by. ASC means [b]Asc[/b]ending order. If your want your result to be in [b]desc[/b]ending order your'll use [b]DESC[/b] instead of ASC

Share this post

Link to post
Share on other sites


Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.