Jump to content

Sorting Retrieved Data


Anakin

Recommended Posts

Hi,

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);

**************************************************
Link to comment
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
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

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.