Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Wanet Telecoms Ltd on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Order by 2 columns? 2

Status
Not open for further replies.

smashing

Programmer
Oct 15, 2002
170
US
I have 2 columns in a table named "which" and "when"

I inserted via phpMyAdmin:
"d" into "which"
"303 West 34th St. cor. 8th Ave, NYC - 5:30 pm" into "when"

I then inserted another row:
"d" into "which"
"303 West 34th St. cor. 8th Ave, NYC - 6:00 pm" into "when"

When browsing the table (I only use phpMyAdmin) it looks like it has put the 6:00 row before the 5:30 row in the table, hmmm I didn't know this is possible so...

I have to order by the "which" column 'cause I have many other entries before these in the table, (some of which are "a" "b" etc in the which column) but:

SELECT * FROM $table order by which ASC

displays the 5:30 before the 6:00 !

I'm trying to order by both columns. Is there such a thing?
Please help I'm going bonkers!!!!



 
Code:
SELECT * FROM $table ORDER BY `which` ASC, `when`

This will do the job ;-)
 
OOOP'S!!! after a closer look I see taht I can't do it this way because of other entries in the when column that should NOT be sorted ASC. How do I fix just this one order of 5 and 6?
 
Ok I see that using phpMyAdmin you have to outsmart it by adding different information, in my case I add a line ending in 2:00 pm.
Then once its in the table you go in again & edit it. Otherwise it sorts the table before the insertion and inserts the information where IT thinks is approriate
Funny but true!!!
 
Records in database are added in order you add them in. If you delete one record, you have to optimize the table so the records after the deleted one are moved one record ahead. If you only update some record, it remains on it's same place. Therefore you can't rely on the order in which the records are added into the DB and you should always use ORDER BY if you need to list the records on the screen.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top