We're a community of 1077K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,076,355 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Sorting Table Data

Hi,

I would like to know if there is a way to sort data in a table, not when selecting it, but the actual data in the table. I would like to sort it by 2 columns though. For example the "topic" should go from A-Z and then inside all rows with a topic of "A" should be sorted by the book_title... So basically priority 1: sort by topic, priority 2: sort by title...

Thanks,
Ashton.

3
Contributors
5
Replies
10 Hours
Discussion Span
2 Years Ago
Last Updated
6
Views
AshtonHogan
Posting Whiz in Training
210 posts since Jul 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

How would you know that data are sorted? MySQL does not guarantee you any sequence of records if you do not use an ORDER BY clause.
What you can do, though, is to create a new table and SELECT your data INTO this table in a certain sort order.

smantscheff
Nearly a Posting Virtuoso
1,297 posts since Oct 2010
Reputation Points: 321
Solved Threads: 270
Skill Endorsements: 8

the concept of sorting the data in the table is meaningless, as you can only see the data when you query the table. so use an order by clause and for all practical purposes, you will image that your table is sorted.

When you delete a few rows and then insert new ones, they could go anywhere in the table, but you'd never know.

What purpose do you imagine that a "sorted" table would server?

drjohn
Posting Pro
508 posts since Mar 2010
Reputation Points: 76
Solved Threads: 99
Skill Endorsements: 4

Hi drjohn,

Ya you actually have a good point... so in that case, is it possible to SELECT data using 2 columns? Or would I have to sort it using ASC for the topics then use code on the back-end to sort that?

AshtonHogan
Posting Whiz in Training
210 posts since Jul 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

of course, select whatever you want from a table, and put in as many or few order by bits as you need.

select col1, col2, col3, col4, col5, col5
from mytable
order by col2 desc, col5 asc;

or whatever suits you

drjohn
Posting Pro
508 posts since Mar 2010
Reputation Points: 76
Solved Threads: 99
Skill Endorsements: 4

NICE! awesome man! thanks so much - works like a charm!

AshtonHogan
Posting Whiz in Training
210 posts since Jul 2009
Reputation Points: 7
Solved Threads: 1
Skill Endorsements: 0

This article has been dead for over three months: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
View similar articles that have also been tagged:
 
© 2013 DaniWeb® LLC
Page rendered in 0.0693 seconds using 2.67MB