Jump to content


Sorting Retrieved Data

  • Please log in to reply
1 reply to this topic

#1 Anakin

  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 24 April 2006 - 04:16 PM


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("SELECT city_id, City FROM city WHERE country_id =".$country_id);


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


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

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


#2 wildteen88

  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 24 April 2006 - 05:08 PM

The ORDER BY 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 FROM city WHERE country_id ='$country_id' ORDER BY city ASC [!--sql2--][/div][!--sql3--]
change city to the row name that you want your sql query to order the results by. ASC means Ascending order. If your want your result to be in descending order your'll use DESC instead of ASC

0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users