GIDForums  

Go Back   GIDForums > Computer Programming Forums > MySQL / PHP Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rating: Thread Rating: 3 votes, 4.33 average.
  #1  
Old 30-Sep-2003, 17:24
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road

How to sort in MySQL?


Now for any easy question
Can I select from a table and sort the data from a column and then sort again from another column?

An example to clear the mud (waffle)
PHP Code:

Select * from table sort by column a 



Now the data in column a is sorted into data X and data Y
Can I now sort the data X and data Y by column b so that data X is in order and data Y is in order?

I tried
PHP Code:

select * from table GROUP BY column a SORT BY column b 


But that didnt work although I may be misunderstanding the commands.
  #2  
Old 01-Oct-2003, 05:59
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough

Sorting multi columns in a MySQL table with ORDER BY


You can sort multi columns easily in a table / SQL Select statement using MySQL's ORDER BY clause.

So, when you want to sort or 'order by' multi columns, you just list the coloumn names in the order of their precedence (left > right), each separated by a comma.

Let's say you have a table like this:
Code:
TABLE NAME: `members` ===================================== id | lastname | firstname | pts -----+------------+------------+----- 1 | Smith | Kate | 105 2 | Smith | Will | 99 3 | Jones | Tommy L | 100 4 | Presley | Lisa | 102 5 | Presley | Elvis | 50
If you run:
PHP Code:

<?php
$sql = 'SELECT `lastname`, `firstname`
       FROM `members`
       ORDER BY `lastname`';
?>

You'd get:
Code:
lastname | firstname -----------+------------ Jones | Tommy L Presley | Lisa Presley | Elvis Smith | Kate Smith | Will
But, notice that the results are less than perfect especially when you consider the 'Presleys'. To sort / ORDER BY firstnames too, you could add the firstname column to the ORDER BY clause, like this:
PHP Code:

<?php
$sql = 'SELECT `lastname`, `firstname`
       FROM `members`
       ORDER BY `lastname`, `firstname`';
?>

Now the results are much better; they would look something like this:
Code:
lastname | firstname -----------+------------ Jones | Tommy L Presley | Elvis Presley | Lisa Smith | Kate Smith | Will
If you ever wanted to sort the lastname ASC (i.e. A through Z) and firstname DESC (i.e. Z through A)... Try:
PHP Code:

<?php
$sql = 'SELECT `lastname`, `firstname`
       FROM `members`
       ORDER BY `lastname` ASC, `firstname` DESC';
?>

Which would result in something like this instead:
Code:
lastname | firstname -----------+------------ Jones | Tommy L Presley | Lisa Presley | Elvis Smith | Will Smith | Kate
  #3  
Old 01-Oct-2003, 07:50
misunderstood misunderstood is offline
Member
 
Join Date: Jun 2003
Posts: 121
misunderstood is on a distinguished road
Ah I see why when I tried that one it didnt work for me! I missed the ' ' around the extra column.
Now a further extension to this. Part of my information (numerical) is pulled from a table and uses another php document for the awarding of points.
ie 2 in my goals column and 1 in my assist column, points column is set at 0. Using my points.php this awards 2 points for each goal and 1 point for each assist. Total = 5 points
PHP Code:

$points=points($hid,$se); 



$points is included in the row recall.
Can I now get all the rows to sort by points?
I suspect that I can by using an array but I am unsure of how i go about it.
There again I am probably barking up the wrong tree! :0
  #4  
Old 01-Oct-2003, 09:01
JdS's Avatar
JdS JdS is offline
Senior Member
 
Join Date: Aug 2001
Location: KUL, Malaysia
Posts: 3,371
JdS will become famous soon enough
Try explaining that again, couldn't quite work it out in my head!

It might be wiser to start a whole new thread if this 2nd question is unrelated to the first post / issue.
 
 

Recent GIDBlogFirst week of IA training by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Windows: From only £20p/y,Linux: from $10p/m. ASP, ASP.NET, PHP, Free MySQL, +More EyotaHosts Web Hosting Advertisements & Offers 0 28-Jun-2003 13:54

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 20:27.


vBulletin, Copyright © 2000 - 2008, Jelsoft Enterprises Ltd.